C++ Memory Map

Since pointers have a close relationship with memory, as they store the address of a memory location and also facilitate the dynamic memory allocation routines of C++, we must understand the way C++ organizes memory for its programs. After compiling a program, C++ creates four logically distinct regions of memory that are used for four distinct and specific functions.

Dynamic and Static Allocation of Memory in C++

The golden rule of computers states that anything and everything (data or instructions) that needs to be processed must be loaded into internal memory before its processing takes place. Therefore, every piece of data and instruction that is being executed must be allocated some space in the main (internal) memory. The main (internal) memory is allocated in these two ways:

Static Memory Allocation in C++

When the amount of memory to be allocated is known beforehand and the memory is allocated during compilation itself, it is referred to as "static memory allocation." For example, when you declare a variable as follows:

short avar;

The computer knows what the length of a short variable is in this case. Generally, it is 2 bytes. So, a chunk of 2 bytes of internal memory will be allocated to the variable "avar" during compilation itself. Thus, it is an example of static memory allocation.

Dynamic Memory Allocation in C++

When the amount of memory to be allocated is not known beforehand, but rather it is required to be allocated as and when required during runtime (when the program is actually executing), the allocation of memory at run time is referred to as "dynamic memory allocation."

C++ offers these two operators for dynamic memory allocations:

The operator "new" allocates the memory dynamically and returns a pointer storing the memory address of the allocated memory. The operator "delete" deallocates the memory (the reverse of new) pointer by the given pointer.

The article "C++ pointers" already contains a section titled "Dynamic memory allocation in C++" that goes into detail on the subject.

Free Store

For dynamic memory allocation, programs are given access to a pool of unallocated heap memory known as "Free Store."

Every program is provided with a pool of unallocated heap memory that it may utilize during execution. This pool of available memory is referred to as the "free store" of the program.

The allocated free store memory is unnamed. Objects allocated in the free store are manipulated indirectly through pointers. Another aspect of free storage is that the allocated memory is uninitialized. The programmer is responsible for initializing it explicitly.

The free store memory is allocated through the use of the new operator and deallocated through the delete operator.

Free store memory is dynamically allocated during runtime, and static memory allocation takes place during compile time. The object's extend is defined as the length of time an object remains in memory during the execution of a program.Global variables or variables with file scope are referred to as having a static extent. That means the storage is allocated to them before the program's start-up and remains bound to the variable throughout the program's execution.

Variables having local scope are spoken of as having local scope. Storage is allocated to them at each entry into the local scope (i.e., as soon as their local scope starts), and on exit (from the local scope), the storage is freed up. A static extend is a local variable with a static specifier.

Note: The storage allocated through the use of a new operator remains bound to the object until it is explicitly deallocated by the programmer.

Objects with dynamic extend are those that are dynamically allocated on free store.

That means the dynamically allocated objects do not have any predefined scope. They are kept in memory until they are explicitly removed with delete.

C++ Quiz

« Previous Tutorial Next Tutorial »

Liked this post? Share it!