Re: stack and a heap

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance




"Gino Cerone" <cosine@xxxxxxxxx> wrote in message
news:u%23P6q8iaFHA.796@xxxxxxxxxxxxxxxxxxxxxxx
> When an application is opened Windows allocates the full 32 bit address
> space (4 gigs) of memory. (this is theoretical and Windows memory manager
> maps from the actual memory to the 32 bit memory space) It sub-divides
> this
> into two parts, the Stack and Heap. The stack holds the declared named
> varibles. In the case of a primative data type like 'byte' it holds the
> value,
>
> byte mybyte;
> mybyte = 125;
>
> In the case of instantiating a class, the declared name varible is writen
> to
> the stack. The instaniated Object in writen to the Heap. The value of the
> varible in the stack holds the address space of the Object in the Heap.
>
> sqlconnection myconnection; (declares the varible myconnection of type
> sqlconnection in the stack. If you tried to use myconnection
> now you would get a null
> reference error because there isn't a memory address in the value yet.)
>
> myconnection = new sqlconnection; (this line instantiates the class
> 'sqlconnection' into an Object in the Heap, and enters the address
> of the Object in
> the value for myconnection. myconnection becomes the pointer that
> references
> the Object
> 'sqlconnection' in the Heap.)
>
>
> "shine" <shine@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:5D2373B4-7795-4B56-9008-E97E74A54690@xxxxxxxxxxxxxxxx
>> what is the difference between a heap and a stack?
>
>

Sorry, but you present a very simplistic view of the real process memory
space and you got it wrong on several points.
1. The full 32 bit address space is not allocated as user process space,
only a maximum of 2GB or 3GB (using /3GB RAM tuning switch in boot.ini) can
ever be reserved (and possibly committed).
2. This space is not sub-divided into a stack and a heap, there is stack
space allocated per thread created and there is heap space. When the OS
creates a process it always starts with a single thread so your process
always start with a single stack (default size: 4KB committed, 1MB reserved
space).
The default sizes of heap and stack space is determined by:
- the built process - managed code generators generate defaults (see later),
using unmanaged code build tools (like link.exe) one can specify other
values for both heap and stack.
- the values assigned by tools like editbin....
The default sizes for ".NET" processes is:
for the heap 4KB committed and 1MB reserved, extra space can (and will)
be requested by the process at run time.
for the stack 4KB committed and 1MB reserved, the stack can never grow
beyond the reserved maximum.

3. The stack only holds local variables, not all "the declared named
variables".
4. Primitive types (as all value types) are only stack allocated when
declared local (they have function scope), instance variables are GC heap
allocated. Note that locals can also be register allocated!

5. This:
> sqlconnection myconnection; (declares the varible myconnection of type
> sqlconnection in the stack. If you tried to use myconnection
> now you would get a null
> reference error because there isn't a memory address in the value yet.)
is not completely true, the compiler will not allow you to use an
uninitialized local variable. If he would allow it, the variable could hold
any possible value that was left on the stack at the location of the
variable.

Willy.


.



Relevant Pages

  • Re: "Physical" memory bounds
    ... This gets tedious with discontiguous memory but I don't see ... is placed in the System Heap. ... Out of this, each task's stack is ... The dynamic memory allocator base allocator will use this label as the ...
    (comp.arch.embedded)
  • 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: 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)
  • Re: Linked List & Dynamic Memory Allocation
    ... Both of you mentioned stack and heap in this ... when I call malloc it uses heap to allocate memory. ... if you have an integer pointer object that lives beyond this scope and you had: ...
    (microsoft.public.vc.mfc)