[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.
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
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.
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.