Reputation: 970
I did the following code to check the data allocation in the memory. I have a RAM of 4 GB
and I learned that the external varibles are stored on the static data space of the memory and auto varibles are on the stack.
A gcc -v
command gives Thread model: win32
. If I use the auto_array
, the programming is crashing. But it doesn't happen with the ext_array
. How I would know the exact size of stack
and static data space of the memory? Is there any other factors affecting the allocation?
#include <stdio.h>
#define MB 1024*1024
char ext_array[1*1024*MB];
int main()
{
//char auto_array[10*MB];
return 0;
}
Upvotes: 0
Views: 111
Reputation: 20980
This is related to stack size.
For global array, the section in data segment is pre-allocated, at program start. Whereas, for auto variables, it gets allocated when the function (main in your case) gets called.
Based on your linker command file, the maximum stack size will be defined. If that size is less than 1 GB, then at the time of function call, the stack frame will get allocated, which would typically encroach into heap.
If you have to use auto variable, check the linker command file for your compiler & see if you can edit the stack size.
Upvotes: 3