Stack Vs. Heap

Some people that I meet have different understanding about stack and heap. So I want to ask you members of http://cplusplus.com/ what is the difference between these two?

Since I'm posting this in the lounge section, I want to ask you this for fun.
If you were a data, where would you go, the stack or heap?


If this does not make sense to you please excuse my ignorance. I really don't know the difference between these two despite reading vague articles about it.
closed account (z05DSL3A)
I would be on the heap. My parents 'newed' me and when I go out of scope I will be deleted. ;0)
Wait a sec. Why would you get deleted when you are dynamically allocated by your parents? You should be deleted when someone killed (free/deleted) you.

Another question, will you be trap in the heap forever if your parents lost the pointer to you?

Will dynamically allocated people(data) be killed(free/deleted) when the world ends (program ends) even if no one killed them during the existence of earth(execution time)?
closed account (z05DSL3A)
I am an active object in the universe thread, my parents don't need to keep track of me and I will be cleaned up when my work is done (or I'll be a ghost in the machine).
The stack is where temporary stuff, like function arguments and local variables, goes. A stack is a special structure that the CPU architecture directly supports, so it is relatively fast and efficient.

The heap is ordinary global memory assigned to your program for dynamic use.

Read more:
http://en.wikipedia.org/wiki/Dynamic_memory_allocation
http://en.wikipedia.org/wiki/Stack-based_memory_allocation

Hope this helps.
@Duoas
Yes that helps, I just really need to improve my english vocabulary. Btw kindly answer this if you could please. thanks again.
Will dynamically allocated people(data) be killed(free/deleted) when the world ends(program ends) even if no one killed them during the existence of earth(execution time)?
closed account (1yR4jE8b)
absolutely
Yes; when a program ends the OS marks all of it's pages as free. Probably.
So it's a yes. Thanks darkestfright, now it's already clear.

Conclusion:
I can allocate dynamic memory and I do not need to delete it manually (though that's bad) because I am guaranteed that it will be cleaned up when the program ends.


One more thing. When a program prints the location a pointer points to, and the another program does the same and we see the same output. Does that mean these two programs are accessing the same memory? Well base on my experimentation it's not, so if it's not how can a program access a dynamically allocated memory of another program?
@chrisname
Yes; when a program ends the OS marks all of it's pages as free. Probably.

You said "probably", so I'm not guaranteed that my garbage will not be cleaned up and my client will get mad at me?
closed account (1yR4jE8b)
Well, if the Operating System is written properly, it will automatically free all of the allocated memory when the program ends, you can basically assume yes, unless it's a POS os.
Well I wouldn't advise someone to assume that. Not to mention that if you don't free your [edit: dynamic] pointers you'll look like an idiot as soon as anyone looks at your code...
Last edited on
Hold on a second... there are differences between memory being simply released and having the objects in memory there properly destructed.

The universe/OS keeps track of what memory is allocated to the program, but it does not care what objects are managed within that local context.

The program handles proper disposal (and garbage collection) of its local objects. If the compiler was well-written (and the program too, of course), the program's objects will be properly disposed when it terminates.

If the program is summarily terminated, that's another thing...
Okay so it is not 100% guaranteed. Now can anyone of you kindly answer my previous question? Thanks.
When a program prints the location a pointer points to, and the another program does the same and we see the same output. Does that mean these two programs are accessing the same memory? Well base on my experimentation it's not, so if it's not how can a program access a dynamically allocated memory of another program?
That's called the flat memory model. Basically, a program is 'told' it has memory locations 0x00000000 <-> 0xFFFFFFFF (Edit: or would it be 0x00000000 <-> 0xXXXXXXXX where XXXXXXXX is the last memory location it could access? Can someone clarify, please?) in it's address space when really it might only have 0xFF7C00BB <-> 0xFFAC00BB. The MMU (Memory Management Unit) on the CPU translates each memory access into it's real one.

Mandatory Wikipedia Link: http://en.wikipedia.org/wiki/Flat_memory_model

The purpose of it is so that programs don't have to compute offsets themselves. They can just pretend their memory starts at 0.

Edit 2: I have a feeling you're going to ask "But what did they do before that was invented?"
They didn't have multitasking then; only one program could be in RAM at a time (with the OS in ROM) so they actually did get to trample the entire address space.

I believe Intel processors have used it (through segmentation) since at least the 8086, although I'm sure it predates the x86.

Edit 3: Of course, the above hexadecimal integers assume 32-bit addressing...
Last edited on
@Duoas
Hold on a second... there are differences between memory being simply released and having the objects in memory there properly destructed.
Can you explain further?

The universe/OS keeps track of what memory is allocated to the program, but it does not care what objects are managed within that local context.

The program handles proper disposal (and garbage collection) of its local objects. If the compiler was well-written (and the program too, of course), the program's objects will be properly disposed when it terminates.
Hmmm, that make sense to me. By saying "properly dispose" you mean manually delete the allocated memory right?

If the program is summarily terminated, that's another thing...
What is it?

Now I'm confuse again by Duoas's statement


[edit]
Grey Wolf wrote:
I am an active object in the universe thread, my parents don't need to keep track of me and I will be cleaned up when my work is done (or I'll be a ghost in the machine).
Does anyone know what he mean by active object because a quick google search didn't give information about active object.
Last edited on
closed account (z05DSL3A)
An Active Object is the object oriented counterpart of the thread abstraction in the procedural world.
Topic archived. No new replies allowed.