In addition to the code and globals memory areas, the program uses other two different areas: the heap, where dynamically allocated variables are pointed to, and the stack, where parameters and local variables are allocated.
The heap (or free store) is a large pool of memory used for dynamic (by new operator) allocation. In C++, when you use the to allocate memory, this memory is assigned from the heap.
The memory allocated has to be accessed indirectly (by pointers) because the exact location of the memory allocated is not known at compile time. When a dynamically allocated variable is deleted, the memory is returned to the heap and can then be reassigned in the future.
The call stack is a fixed-size chunk of sequential memory addresses. The stack pointer (a register in the CPU) tracks the top of the stack at each moment.
The stack is used mainly to keep track of the point to which each active function should return control when it finishes executing. Meanwhile, the stack stores function data like parameters and local variables.
The stack has a limited size, and consequently can only hold a limited amount of information. Stack overflow is generally the result of allocating too many variables on the stack, and/or making too many nested function calls.
All memory allocated on the stack is known at compile time. So, this memory can be accessed directly through a variable.
Closely related to the above, the storage duration (or storage class) determines the duration of a variable in a program execution, in other words, when it is created and when it is destroyed. They are 3 types of storage durations:
Automatic storage duration: Variables inside a function (including function parameters) or block are created when the program enters the function/block in which they are defined, and destroyed when the program leaves the function/block. It's not necessary a programmer's explicit action.
Automatic variables are not initialized and typically allocated in the stack.
Static storage duration: Variables declared outside all functions, or inside a function with static specifier exist for the entire duration of the program and they retain their memory and contents throughout the entire execution. Static variables are allocated at fixed memory locations.
Dynamic storage duration: Dynamic memory is managed by programmer explicitly and allocated on the heap via new operator and persists until it is freed via the delete operator.