Re: where do the automatic variables go ?
- From: Antoninus Twink <nospam@xxxxxxxxxxxxxx>
- Date: Sat, 9 Aug 2008 19:53:08 +0000 (UTC)
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.
.
- Follow-Ups:
- Re: where do the automatic variables go ?
- From: Chris M. Thomasson
- Re: where do the automatic variables go ?
- From: Chris M. Thomasson
- Re: where do the automatic variables go ?
- From: sidd
- Re: where do the automatic variables go ?
- References:
- where do the automatic variables go ?
- From: sidd
- where do the automatic variables go ?
- Prev by Date: Re: how to find number of bytes freed
- 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
|