Re: where do the automatic variables go ?
- From: Robert Heller <heller@xxxxxxxxxxxx>
- Date: Sun, 10 Aug 2008 14:42:52 -0500
At Sun, 10 Aug 2008 07:30:20 -0700 (PDT) sidd <siddharthkumra@xxxxxxxxx> wrote:
On Aug 10, 5:34=A0pm, Robert Heller <hel...@xxxxxxxxxxxx> wrote:
At Sun, 10 Aug 2008 03:37:14 -0700 (PDT) sidd <siddharthku...@xxxxxxxxx> =wrote:
t
In the following code:
int i =3D 5; =A0---> it goes to .data segment
int j; =A0 =A0 =A0 =A0---> it goes to bss segment
int main()
{
int c;
int i =3D 5; ---> stack
int j[5] =3D new int[5]; ----> heap
c =3D i*2; ----> goes to .text segment
}
My question is : When the object file is created there are text, data
and bss segments etc...but there is notthing like stack and heap
segment, =A0what happens to these automatic variables ? In which segmen=
ox Toolbar!are they located in a.out file
They aren't in the a.out file and don't even exist until run time.
Stack variables are implied by the compiler (the compiler generates code
that pushes them onto the stack).
Heap variables are created at run time by a new (or malloc) call
(run time function call).
Thanks,
Siddharth
--
Robert Heller =A0 =A0 =A0 =A0 =A0 =A0 -- Get the Deepwoods Software FireF=
Deepwoods Software =A0 =A0 =A0 =A0-- Linux Installation and Administratio=nhttp://www.deepsoft.com/=A0-- Web Hosting, with CGI and Database
hel...@xxxxxxxxxxxx =A0 =A0 =A0 -- Contract Programming: C/C++, Tcl/Tk- H=ide quoted text -
- Show quoted text -
Can you please explain a bit more as to what you mean by saying "Stack
variables are implied by the compiler"
Stack variables are allocated at run time by compiler generated code.
Here is an example:
A Simple C/C++ function:
int foo(int i, int j)
{
int bar = 5;
return i*j+bar;
}
Here is some simplified psuedo assembly code that might be generated:
* int foo(int i, int j)
* Stack on input:
*
* [j]
* [i]
* [return address]
* SP-> [ ]
foo: push #5 bar = 5 Bar is allocated by
* compiler generated code
* at run time. Space for
* bar does not exist in the
* a.out file. Instead it
* is created by run time
* code.
move -3(SP),R0 R0 = i (-3(SP) is i)
mul -4(SP),R0 R0 = i*j (-4(SP) is j)
add -1(SP),R0 R0 = i*j+bar (-1(SP) is bar)
pop pop bar off the stack
exch -1(SP),-3(SP) exchange return address and j
pop pop j off the stack
pop pop i off the stack
ret Return, result in R0
Note: a smart, optimizing compiler would optimize bar completely away
in this function. In this case the use of a variable here is
*obviously* silly, but even in cases where it is not obvious, the
compiler might still optimize a local variable away. In other cases,
it might get mapped to an available CPU register. How memory is
allocated for a local / automatic variable is pretty much up to the
compiler. Such variables (in C and C++ anyway -- other languages have
different semantics for 'local' variable -- FORTRAN (f77) normally
considers all local variables to be 'static' for example, although the
optimizer might do something else) are never allocated in the a.out
file.
Thanks
--
Robert Heller -- Get the Deepwoods Software FireFox Toolbar!
Deepwoods Software -- Linux Installation and Administration
http://www.deepsoft.com/ -- Web Hosting, with CGI and Database
heller@xxxxxxxxxxxx -- Contract Programming: C/C++, Tcl/Tk
.
- Follow-Ups:
- Re: where do the automatic variables go ?
- From: sidd
- Re: where do the automatic variables go ?
- References:
- Re: where do the automatic variables go ?
- From: Robert Heller
- Re: where do the automatic variables go ?
- From: sidd
- Re: where do the automatic variables go ?
- Prev by Date: What does a dpi daemon do?
- Next by Date: Re: jsp files not working
- Previous by thread: Re: where do the automatic variables go ?
- Next by thread: Re: where do the automatic variables go ?
- Index(es):
Relevant Pages
|
Loading