Memory Layout of C++
Memory Layout of C++ : Let us understand how C++ organizes memory for its programs. After compiling a program, C++ create four logically distinct regions of memory that are used for different distinct specific functions. These are :
- Program Code Region
- Global Variables Region
- Heap Region
- Stack Region
- Program code region :
This region in the memory holds the compiled code of the program. The program’s every instruction and the program’s every function starts at a particular address.
- Global variables region :
This region of the memory stores the Global variables of the program. Global variables remain in the memory as long as program/application/process continues.
- Stack region :
This region is used for great many things while your program executes. The stack holds
- the return addresses at function calls
- arguments passed to the functions and
- local variables for functions. Local variables remain in memory as long as the function continues and after that they are erased from the memory.
- This region also stores the current state of the CPU.
- Heap memory region :
The heap memory area is a region of free memory from which chunks of memory allocated via C++ dynamic memory allocation functions.
Concept of Free store :
- Heap memory region contains a linked list of used and free blocks and every program is provided with a pool of unallocated heap memory i.e. free blocks so that it may utilize during execution. This pool of free blocks or available memory or unallocated heap memory is referred to as the program’s free store.
Free store is a pool of unallocated heap memory or free blocks in heap memory given to a program that is used by the program for dynamic allocation during execution.
- The allocated free store memory is unnamed and the objects allocated on the free store are manipulated/accessed indirectly through Pointers.
- The objects allocated on the free store memory is un-initialized and carries garbage value. It will be explicitly initialized by the programmer.
- The free store memory is allocated using new operator and deallocated using delete operator.
Free store memory is (dynamically) allocated during runtime and static memory allocation takes place during compile time.
Lifetime of objects
The object’s extent or the object’s lifetime is the time of the object remains in the memory during the program execution.
- Global variables or the variables having file scope are spoken of as static extent and therefore the storage allocated before the program’s startup to the global variables and remain in the memory as long as program/application/process continues..
- Variables having local scope has spoken of as local extent. Storage is allocated to the variables as soon as their local scope starts and deallocate the memory on exit ( from the local scope). Local scope with static specifier has static extent.
The storage is allocated for the use of operator new remains bound to the object until it is explicitly deallocated by the programmer.
- That means the dynamically allocated objects do not have any predefined scope and remains in memory until explicitly removed using delete.
Heap vs. Free Store:
|A dynamic memory area that is allocated/freed by the malloc/free functions.||A dynamic memory area that is allocated/freed by new/delete.|
|malloc and free do not call the constructor and destructor respectively.||new and delete call the constructor and destructor, respectively|
Reference : http://www.gotw.ca/gotw/009.htm
The free store is one of the two dynamic memory areas, allocated/freed by new/delete. Object lifetime can be less than the time the storage is allocated; that is, free store objects can have memory allocated without being immediately initialized, and can be destroyed without the memory being immediately deallocated. During the period when the storage is allocated but outside the object’s lifetime, the storage may be accessed and manipulated through a void* but none of the proto-object’s nonstatic members or member functions may be accessed, have their addresses taken, or be otherwise manipulated.
The heap is the other dynamic memory area, allocated/freed by malloc/free and their variants. Note that while the default global new and delete might be implemented in terms of malloc and free by a particular compiler, the heap is not the same as free store and memory allocated in one area cannot be safely deallocated in the other. Memory allocated from the heap can be used for objects of class type by placement-new construction and explicit destruction. If so used, the notes about free store object lifetime apply similarly here.
Incoming search terms:
- memory layout in c
- memory layout of objects in c
- c object memory layout
- memory layout of c program
- memory layout of c identify dynamic region
- memory layout of a cpp program
- memory layout for c
- memory diagram in c
- memory lyoit in c
- c program memory layout