LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Organization of local variables on the stack with gcc (http://www.linuxquestions.org/questions/programming-9/organization-of-local-variables-on-the-stack-with-gcc-923295/)

wtruong 01-11-2012 01:40 PM

Organization of local variables on the stack with gcc
 
Can anyone explain to me or point me to an article describing how local variables are mapped in memory?

For instance on a 32 bit linux system using gcc:

2.6.18-194.32.1.el5 #1 SMP Wed Jan 5 17:53:09 EST 2011 i686 i686 i386 GNU/Linux

gcc version 4.1.2

Code:

void foo()
{
  int x;
  char a[20];
  int y;
  char b[16];
}

(gdb) p &x
$1 = (int *) 0xbf957b50
(gdb) p &a
$2 = (char (*)[15]) 0xbf957b41
(gdb) p &y
$3 = (int *) 0xbf957b54
(gdb) p &b
$4 = (char (*)[16]) 0xbf957b31


neonsignal 01-12-2012 03:48 AM

Local variables are stored on the stack, and typically addressed through a base pointer. If you have optimization on (as you appear to in your example), the variables may be reordered, so that the offsets can be minimized (notice that the two integers have been placed near the base pointer).

NevemTeve 01-12-2012 03:59 AM

The compiler organizes the variables as it thinks fit, don't have expectations... It may sort them by name, type, size, declaration order, usage frequency, or even karma.

theNbomr 01-12-2012 10:57 AM

Quote:

Originally Posted by NevemTeve (Post 4572601)
The compiler organizes the variables as it thinks fit, don't have expectations... It may sort them by name, type, size, declaration order, usage frequency, or even karma.

Thank you for making that clear.
There is a common assumption that the C language specifies something about how local (or any other) variables are stored. While it is common to use the stack, unless the compiler vendor specifically documents any particular organization, you cannot rely on any particular arrangement, nor can you assume that it will be consistent for any particular set of cases such as optimization levels, compiler versions, target architecture, phase of the moon, etc. Some CPUs don't even have a stack, and the compiler must use other means to create local variable storage.

--- rod.

wtruong 01-12-2012 11:25 AM

Undefined. Thanks for your quick reply.

neonsignal 01-12-2012 06:40 PM

Quote:

you cannot rely on any particular arrangement, nor can you assume that it will be consistent for any particular set of cases such as optimization levels, compiler versions, target architecture, phase of the moon
While that's true, the original question specified a particular compiler and architecture.

The C standard defines very little in the way of implementation for any part of the language, and for good reason; it is a definition of a high level language, not of a compiler.

But with gcc on a x86 architecture, it is typical that local variables are on the stack (or in registers, or optimized away). Once you bring in variations (optimizations, compilers, architectures, stack security, etc), the exact implementation can become quite complicated, beyond the scope of a short forum response, but it is still deterministic, it has nothing to do with 'karma' or 'phase of the moon'!


All times are GMT -5. The time now is 01:57 AM.