ikerhurtado.com
You're in
Iker Hurtado's pro blog
Developer | Entrepreneur | Investor
Software engineer (entrepreneur and investor at times). These days doing performant frontend and graphics on the web platform at Barcelona Supercomputing Center

Relearning C++: Notes on the heap, the stack and the storage duration

6 Mar 2015   |   iker hurtado  
Share on Twitter Share on Google+ Share on Facebook
In this post I write down some notes as from my study of C++ on the memory organization and use in a executing program

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

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.

Because the heap is a big pool of memory, large arrays, structures, or classes should be allocated here.

The stack

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.

Because the stack is relatively small, it is generally not a good idea to allocate large arrays, structures, and classes, as well as to be careful with recursion.

Storage Duration

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.


POST A COMMENT: