Stack vs heap
The stack holds automatic objects: locals and parameters whose lifetime ends when the scope ends. The heap holds dynamic objects: memory you get from new, malloc, or an allocator, whose lifetime ends when you release it. C++ objects can live in either place.
Detailed Explanation
A function's locals sit in a contiguous frame the CPU grows and shrinks with sp. Construction runs when the declaration is reached. Destruction runs in reverse when the scope leaves, including on throw. That is why RAII works for locals: the destructor is tied to the frame. Stack allocation is a pointer bump. It is fast. The size of a frame is usually known at compile time (VLAs are not standard C++). A huge char buf[10'000'000] on the stack will overflow the thread's stack (often 1–8 MB). Put large buffers on the heap.
The heap is a general allocator. new T finds a block, runs T's constructor, and returns a pointer. The object lives until delete (or until the unique_ptr that owns it dies). Heap allocation takes a lock or an atomic in the allocator, searches free lists, and may ask the OS for more pages. It is slower than a stack bump and it fragments. It is the only place a size known only at runtime can live (vector's buffer, string's long text).
Globals and static locals are neither: they have static storage duration. They live for the program (or until the first pass, for function-scope static). Thread-local storage is per thread.
new is not "the heap keyword" exclusively. Placement new constructs in storage you already have, which might be a stack buffer. std::vector on the stack is a small object (pointer, size, capacity) whose elements are on the heap. People say "the vector is on the stack" and mean the handle.
Ownership is the design question. A stack object is owned by the frame. A heap object is owned by whoever is responsible for delete. Raw owning pointers lose that information. unique_ptr writes it in the type.
Stack overflow is a crash (or silent corruption). Heap exhaustion throws std::bad_alloc from new (unless you used the nothrow form). Use the heap for large or variable data. Use the stack for small, scoped values. Use static storage for truly process-wide state, sparingly.
Escape analysis is not a C++ language feature. If you return a pointer to a local, it dangles. The compiler will not move that local to the heap for you. Return by value and let move/elision do the work.
Code Examples
#include <iostream>
#include <memory>
#include <vector>
int main() {
int n = 7; // stack
std::vector<int> v{1, 2, 3}; // handle on stack, buffer on heap
auto p = std::make_unique<int>(9); // int on heap
std::cout << n << ' ' << v[0] << ' ' << *p << '\n'; // prints: 7 1 9
}int* dangling() {
int x = 1;
return &x; // x dies; dangle
}
int value() {
int x = 1;
return x; // copy/elide; safe
}Common Gotchas
Returning the address of a local
the frame is gone. Return by value or allocate on the heap and transfer ownership.
Large stack arrays
`std::array<int, 2'000'000>` can overflow. Use `vector`.
Heap is not automatically faster for tiny objects
a `unique_ptr<int>` is a heap hit you do not need. Store the `int`.