[SOLVED] Organization of local variables on the stack with gcc
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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).
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.
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.
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'!
Last edited by neonsignal; 01-23-2012 at 03:11 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.