Home | API | MFC | C++ | C | Previous | Next

Programming With C++

Memory Map and Free Store

The memory that a program uses is typically divided into a few different areas:

Global Namespace – contains global variables
The Free Store (heap) – contains dynamically allocated variables 
Registers – are used for internal housekeeping functions, such as keeping track of the stack and the instruction pointer.
Code Space – contains the program.
The Stack – used for storing function parameters, local variables, and other function-related information.

Free Store

The free store is a large pool of unallocated memory used by the program for dynamic memory allocation during the execution of the program.  Objects allocated on the free store are manipulated indirectly through pointers. Free store memory is allocated through the use of the new operator and deallocated through the delete operator.  The advantage of the free store is that reserved memory is available until its explicitly released. If memory is allocated on the free store while in a function, the memory will still be available when the function returns.  The disadvantage of the free store is that reserved memory remains unavailable until its explicitly released. Failure to free this memory can build up over time resulting in memory leak which can result in performance issues and a possible system crash. 

Allocating Space with the New Keyword

Memory is allocated on the free store by using the new keyword followed by the type of the object that is to be created. This tells the compiler how much memory to set aside.  To request new memory for a variable type int using the following -

int *ptr = NULL; - creates a pointer ptr and assigns NULL
ptr = new int; - allocates space for a int variable on the heap and assign its address to pointer ptr

or

int *p = new int

Deallocating Space with the Delete Keyword

When an area of the free store is no longer required, it must be freed back to the system. This is done by calling delete on the pointer.  If a pointer variable is pointing to free store memory and the pointer goes out of scope, the memory is not automatically returned to the free store and becomes unavailable. This is called a memory leak. To release memory back to the free store use the keyword delete - - 

delete pPointer;

The code section below illustrates the allocating and deleting of a pointer

#include <iostream>
using namespace std;
int main()
{
int * pInt= new int;//create pointer pInt
*pInt=7;//assign value 7
cout << "*pInt: " << *pInt << endl;//output value of pInt
delete pInt;//delete pointer
cout << "*pInt after deleting: " << *pInt << endl;//output pointer value
return 0;
}


If there is insufficient space on the heap the request to allocate memory will fail. The new request will be in the form of an exception of type std::bad_alloc.  If “nothrow” is used with the new operator, it will return a NULL pointer. To add a free store request fail exception to the above code segment -

int *pInt = new(nothrow) int;
if (!pInt)
cout << "allocation of memory failed\n";

 


Home | API | MFC | C++ | C | Previous | Next
The Basics | Variables and Constants | Arrays | C-strings | Expressions and Operators | Controlling Program Flow | C++ Functions | Pointers and References | Memory Map and Free Store | Smart Pointers | Classes | Structures | Inheritance | Polymorphism | Templates | The Standard Template Library | The STL String Class | Namespace | Type Conversions | Input and Output Streams | The C++ Preprocessor | Exception Handling

Last Updated: 15 September 2022