LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-11-2012, 01:40 PM   #1
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Rep: Reputation: 16
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

Last edited by wtruong; 01-11-2012 at 01:58 PM. Reason: Listed architecture and gcc version
 
Old 01-12-2012, 03:48 AM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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).
 
Old 01-12-2012, 03:59 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 1,813

Rep: Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503Reputation: 503
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.
 
Old 01-12-2012, 10:57 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Quote:
Originally Posted by NevemTeve View Post
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.
 
Old 01-12-2012, 11:25 AM   #5
wtruong
Member
 
Registered: May 2009
Distribution: ubuntu
Posts: 35

Original Poster
Rep: Reputation: 16
Undefined. Thanks for your quick reply.
 
Old 01-12-2012, 06:40 PM   #6
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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'!

Last edited by neonsignal; 01-23-2012 at 03:11 AM.
 
  


Reply

Tags
memory


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory for local variable in C on stack shivaligupta Programming 10 01-20-2007 01:22 AM
Compiler did not align stack variables. gbowden Linux - Software 2 12-15-2006 07:22 AM
How to get local gcc to link with local glibc dombrowsky Linux - Software 9 01-20-2006 02:01 PM
stack organization questions 0x0000h Linux - Security 2 08-26-2005 11:04 AM
gcc stack problem scoban Programming 8 08-15-2005 11:44 PM


All times are GMT -5. The time now is 10:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration