linux - Memory allocation for C program -


program

#include<stdio.h> int a=10; void main() {     int i=0;     printf("global = %p, local = %p\n",&a,&i);     main(); } 

output

mohanraj@ltsp63:~/advanced_unix/chapter7$ ./a.out global = 0x804a014, local = 0xbfff983c global = 0x804a014, local = 0xbfff980c . . . global = 0x804a014, local = 0xbf7fac9c global = 0x804a014, local = 0xbf7fac6c global = 0x804a014, local = 0xbf7fac3c segmentation fault (core dumped) mohanraj@ltsp63:~/advanced_unix/chapter7$ 

the above program gets segmentation fault error. because, main gets call recursively. following memory allocation c program.

memory allocation

          __________________                        __________________             |                |                        |                |            |      stack     |                        |     main       |           |  ↓             |                        |----------------|           ------------------                        |     main       |            |                |                        |----------------|           |   <un allocated|                        |     main       |           |       space>   |                        |----------------|           ------------------                        |     main       |           |                |                        |----------------|           |    ↑           |                        |     main       |           |       heap     |                        |----------------|           |                |                        |     main       |           |                |                        |----------------|           __________________                        |////////////////| ---> collision occurs. so, segmentation fault occurs.             |                |                        |________________|           |       data     |                        |       data     |           __________________                        |________________|           |       text     |                        |       text     |           __________________                        |________________|                figure(a)                               figure(b) 

so, expect showed in figure(b), main call recursively. if reaches data segment, collision occurs. if occurs, there no more space allocate main function. so, gets segmentation fault error. using above program experiment it. on program, address of global variable 'a' "0x804a014". each time main called, local variable "i" gets declared. so, expect, before segmentation fault, address of address of 'a'. but, both address different. what's here going on.

why address of 'a' , 'i' not in same range @ time of segmentation fault error. so, how cross check whether main reaches stack size , gets overflowed ?

your schema conceptual model or possible implementation. example multithreaded program have 1 stack per thread , 1 single heap, not fit in simplified schema.

all required system allows recursion, meaning each new invocation of functions gets private copy of local variables. remains implementation dependant.

recent system use page allocation, , process gets set of page segments, not consecutive, , can have holes between them access sigsegv (segment violation)

tl/dr: program more sigsegv signal address of dynamic variable reaching address of static 1 - should find old ms/dos box exhibit such behaviour...


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -