c - gcc stack memory allocation -
I am confused with the memory layout assignment which says we are:
int flag = 0; Four array [10];
In the memory stack, the array is located above the flag.
(gdb) x / x array: 0xbffff4bc (gdb) x / x flag: 0xbffff4cc
However, we say that we reverse the lines Given:
four arrays [10]; Int flag = 0;
Still the assignment of memory is the same
(gdb) x / x array: 0xbffff4bc (gdb) x / x flag: 0xbffff4cc
How did the assignment not change? In the second option the flag should not be above the array in the heap? So why is the allocated memory for the array above the integer in the stack event, am I compiling with the -fno-stack-protector
flag? Is this true or not? I am running my code under GCC 4.7.2. Debian
Edit
I have received a position of only one problem, however, while reading the first answer, it says in the comments that when If compile with -fno-stack-protector
, then the problem should be resolved (orderly allocate memory in order of declaration). It does not work in my case though. any suggestion?
As stated by @AndreyT's comment, there is no requirement in the standard that the compiler On the stack, the local variables have to be kept in the same order, as they appear in the source, or in that particular order for that matter.
You can put them sequentially in a structure, because the layout specifies the rules for standard strokes (C99 6.7.2.1 paragraph 13). Even if the compiler is necessary, it is free to put the padding between the elements.
But by the way, your strategy is faulty, because the standard also says that writing out of the array's range is "undefined Behavior" that there is no guarantee that it will update your flag safely. Different architectures and / or compilers may give different responses, perhaps due to hardware trigger exceptions or other fatal conditions. You can not rely on any particular type of trust.
Comments
Post a Comment