memory allocation from stack and heap ?

This is a discussion on memory allocation from stack and heap ? within the C++ Programming forums, part of the General Programming Boards category; Hi,
Can anybody tell me what is the difference memory allocation from stack and heap ?
And how compiler decide ...

The easy explanation:
The stack is the default place of allocation. Any variables you define reside there. They are cleaned up automatically after a function ends.
The stack is limited (around 1 MB).

The heap is essentially the big memory in the system. It's limited only to the amount of memory you have installed[1]. Objects on the heap are not automatically destroyed, so you need to do so manually (thankfully, smart pointers and RAII can take care of that for us in C++). This is good because it outlives the life of a function.

[1] The heap is actually limited to 2/3 GB depending on OS. The OS will handle where the memory is stored. If you don't have enough physical memory, it will page it to the pagefile.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.