Re: where do the automatic variables go ?
- From: sidd <siddharthkumra@xxxxxxxxx>
- Date: Sat, 9 Aug 2008 13:13:52 -0700 (PDT)
On Aug 10, 12:53 am, Antoninus Twink <nos...@xxxxxxxxxxxxxx> wrote:
On 9 Aug 2008 at 18:26, sidd wrote:
int main()
{
int c;
int i = 5; ---> stack
int j[5] = new int[5]; ----> heap
c = i*2; ----> goes to .text segment
}
This is a misleading dichotomy. The new line will almost certainly
generate instructions too, which will be placed in the .text segment:
new needs to call malloc() to get its memory, which will indeed be taken
from the heap.
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, what happens to these automatic variables ?
The kernel provides each process with a virtual address space, and takes
responsibility for mapping this to physical memory. When your C program
has been loaded and starts executing, the operating system
(executable-loader) has kindly set up this address space to look like
this:
highest address
=========
| |
| |
| |
| |
| |
| |
=========
| data |
| +bss |
=========
| text |
=========
address 0
As you program starts generating stack frames and automatic variables,
the stack grows downwards from high to low memory addresses. Meanwhile,
the heap starts growing upwards from above the data segment.
highest address
=========
| stack |
| vv |
| |
| |
| ^^ |
| heap |
=========
| data |
| +bss |
=========
| text |
=========
address 0
Things get more interesting when you add libraries, threads etc. into
the mix, but this is the basic setup.
Thanks for the wonderful links and explanations, but I guess my
question still remains unanswered. My question was that when the code
is compiled and converted to a.out, what happens to the automatic
variables, in other words can someone elaborate more about the
generation of stack frames. I understand that whenever a function is
called, a stack frame gets generated but how are the details of that
routine laid out in the a.out file. In other words where do the
automatic variables part of the routine lie in the a.out format.
Thanks,
Sidd
.
- Follow-Ups:
- Re: where do the automatic variables go ?
- From: Stephen Sprunk
- Re: where do the automatic variables go ?
- From: Keith Thompson
- Re: where do the automatic variables go ?
- From: Antoninus Twink
- Re: where do the automatic variables go ?
- From: Bartc
- Re: where do the automatic variables go ?
- References:
- where do the automatic variables go ?
- From: sidd
- Re: where do the automatic variables go ?
- From: Antoninus Twink
- where do the automatic variables go ?
- Prev by Date: Re: where do the automatic variables go ?
- Next by Date: Re: where do the automatic variables go ?
- Previous by thread: Re: where do the automatic variables go ?
- Next by thread: Re: where do the automatic variables go ?
- Index(es):
Relevant Pages
|