• Advertisement
Sign in to follow this  

Stack and Heap

This topic is 4650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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+.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement