Memory allocation for C program Memory allocation for C program unix unix

Memory allocation for C program


Your schema is a conceptual model or a possible implementation. But for example a multithreaded program will have one stack per thread and one single heap, which does not really fit in your simplified schema.

All what is required is that the system allows recursion, meaning that each new invocation of a functions gets a private copy of local variables. All what remains is implementation dependant.

Recent system use page allocation, and a process generally gets a set of page segments, but they are not necessarily consecutive, and you can have holes between them where any access will get a SIGSEGV (segment violation)

TL/DR: you program will more likely get a SIGSEGV signal than the address of the dynamic variable reaching the address of static one - you should find an old MS/DOS box to exhibit such behaviour...


'a' is a global variable and it will not be in stack. This will be in data section - i.e., initialized bss

'i' is a local variable and will be stored in the stack.

These are entirely different sections and hence the difference.

Please refer Global memory management in C++ in stack or heap?


When you wrote variable "i" gets declared, you are writing something correct, but the variable is not declared as the global a.

i is stack allocated, and stack has its own size.

ulimit can change that limit.

What you are trying to see, collision between address allocation of a global variable and a local variable, is not possible.