Stack and Heap

Started by
8 comments, last by silverphyre673 18 years, 10 months ago
I haven't really done to much study on this. I think that the heap is where memory is allocated with pointers. Am I correct? If that's true than the Stack would be the one where Windows limits you to 1 mb.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
Advertisement
The heap is where things are allocated using "new" or "malloc"... this address is then stored in a pointer, however not all pointers lead to the heap as you could setup a pointer to point to a location on the stack. The stack is where just about everything else is stored then. Not sure about the limit though...
When you use new or malloc or one of thier kin it comes from the heap.

When you have a variable directly in a function it comes from the stack.

To use your pointer for example, if you have:

int * p = new int;

the memory for the int you just allocated lives in the heap. The memory for the pointer that points to that int lives in the stack.
-Mike
The stack is also where one 'saves' the values of certain CPU registers (by using 'push' and 'pop'). And you can't insert anything on the stack. What comes on last, must go first. :)
IIRC, the default stack size is 1 meg, yes (for Windows; for POSIX it's probably undefined). You can set that when you create the thread, both on Windows and POSIX.
The stack size for Windows apps is set by the linker (using the /STACK switch). Yes, it defaults to 1MB if you don't use the switch.

For threads, the stacksize you specify is typically the commit size, not the reserve size. The difference being that the reserve size is just how much address space the stack will take. It's not until you commit it that it actually has memory backing it. You can set the commit size instead of the reserve size by giving a magic flag to CreateThread but this only works on XP+.
-Mike
malloc/free work on the heap
new/delete work on the free-store
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Hey, I thought I had pointers out, but I have a quick question: If you have a pointer to something on the stack, do you want to call delete on the pointer when you are finished with it? If you do, will the original be dead? I mean this:

unsigned int OnTheStack;

unsigned int * Pointer = &OnTheStack

delete Pointer; //What happens?
my siteGenius is 1% inspiration and 99% perspiration
You should really really not do that.

I'm pretty sure that "what happens" is "undefined behaviour", which was "defined" by Scott Meyers as:
it works for you, it works for me, but blows up in your most important customers face
[ ThumbView: Adds thumbnail support for DDS, PCX, TGA and 16 other imagetypes for Windows XP Explorer. ] [ Chocolate peanuts: Brazilian recipe for home made chocolate covered peanuts. Pure coding pleasure. ]
Excellent. I will remember that.
my siteGenius is 1% inspiration and 99% perspiration

This topic is closed to new replies.

Advertisement