Re: where do the automatic variables go ?



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.

.



Relevant Pages

  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: run-time vs compile-time
    ... > offset related to some location (like stack base) somewhere. ... > offset from heap to pi. ... When you allocate an int on the heap, it is allocated at address 1. ... application has a given amount of memory it can use as it wishes. ...
    (comp.lang.cpp)
  • Re: Questions about Minix
    ... Of course you still need to allocate chunks of contiguous memory, ... in user mode and at kernel level if you need dynamic ... I read "data segment". ... a stack from growing into the heap (only a possibility of detecting it ...
    (comp.os.minix)
  • Memory problems - WinDbg and SOS: Who recognizes this pattern?
    ... Last night I managed to get a memory dump using ADPlus and I analyzed it ... ephemeral segment allocation context: none ... Large object heap starts at 0x0a0d0030 ...
    (microsoft.public.dotnet.framework.performance)
  • Re: Error Raising and Memory in VB (general question)
    ... > object is terminated go out of scope, and the memory is also released. ... But why are you saying it uses stack? ... I think we are dealing with heap memory here. ... "COM's IMalloc allocator: ...
    (microsoft.public.vb.general.discussion)