Ready to practice?
Sign up to access interactive coding exercises and track your progress.
Manual Memory Management with new and delete
Allocate single objects on the heap and properly release the memory.
Dynamic memory allocation with new and delete
C++ supports three basic types of memory allocation, of which you've already encountered two.
- Static memory allocation happens for static and global variables. Memory for these types of variables is allocated once when your program starts and persists throughout the program's life.
- Automatic memory allocation happens for function parameters and local variables. Memory for these types of variables is allocated when the relevant block is entered and freed when the block is exited, as many times as necessary.
- Dynamic memory allocation is the topic of this article.
Both static and automatic allocation share two characteristics:
- The variable or array size must be known at compile time.
- Memory allocation and deallocation happens automatically (when the variable is instantiated or destroyed).
Most of the time, this works perfectly. However, you'll encounter situations where one or both of these constraints cause problems, usually when dealing with external (user or file) input.
For example, we may want to use a string to store someone's email address, but we don't know how long their email is until they enter it. Or we may want to load a batch of images from disk, but we don't know in advance how many images exist. Or we may be creating a simulation, with a variable number of particles (that changes over time as some particles disappear and new ones are spawned) interacting with each other.
If we have to declare the size of everything at compile time, the best we can do is guess the maximum size of variables we'll need and hope that's sufficient:
char emailAddress[100];
Image loadedImages[200];
Particle simulationParticles[500];
Polygon renderingMesh[15000];
This is a poor solution for at least four reasons:
First, it leads to wasted memory if the variables aren't actually used. For example, if we allocate 100 chars for every email address, but email addresses are on average only 35 chars long, we're using nearly three times what we really need. Or consider the rendering mesh above: if a mesh only uses 5,000 polygons, we have 10,000 Polygons worth of memory not being used!
Second, how do we determine which portions of memory are actually used? For strings, it's easy: a string that starts with a \0 is clearly not being used. But what about simulationParticles[137]? Is it active or inactive right now? Has it even been initialized? That necessitates having some way to track the status of each particle, adding complexity and potentially consuming additional memory.
Third, most normal variables (including fixed arrays) are allocated in a portion of memory called the stack. The amount of stack memory for a program is generally quite small -- Visual Studio defaults the stack size to 1MB. If you exceed this limit, stack overflow will result, and the operating system will probably close down the program.
On Visual Studio, you can observe this happening when running this program:
int main()
{
int hugeArray[2000000];
}
Being limited to just 1MB of memory would be problematic for many programs, especially those dealing with graphics.
Fourth, and most importantly, it can lead to artificial limitations and buffer overflows. What happens when the user tries to load 250 images from disk, but we've only allocated memory for a maximum of 200 images? Either we have to give the user an error, only load the 200 images, or (in the worst case where we don't handle this situation at all) overflow the image array and experience undefined behavior.
Fortunately, these problems are easily addressed via dynamic memory allocation. Dynamic memory allocation is a way for running programs to request memory from the operating system when needed. This memory does not come from the program's limited stack memory -- instead, it is allocated from a much larger pool of memory managed by the operating system called the heap. On modern machines, the heap can be gigabytes in size.
Dynamically allocating single variables
To allocate a single variable dynamically, we use the scalar (non-array) form of the new operator:
new int;
In the above case, we're requesting an integer's worth of memory from the operating system. The new operator creates the object using that memory, then returns a pointer containing the address of the memory that has been allocated.
Most often, we'll assign the return value to our own pointer variable so we can access the allocated memory later.
int* ptr{ new int };
We can then dereference the pointer to access the memory:
*ptr = 42;
If it wasn't before, it should now be clear at least one case in which pointers are useful. Without a pointer to hold the address of the memory that was just allocated, we'd have no way to access the memory that was just allocated for us!
Note that accessing heap-allocated objects is generally slower than accessing stack-allocated objects. Because the compiler knows the address of stack-allocated objects, it can go directly to that address to retrieve a value. Heap allocated objects are typically accessed via pointer. This requires two steps: one to retrieve the address from the pointer, and another to retrieve the value.
How does dynamic memory allocation work?
Your computer has memory (probably lots of it) available for applications to use. When you run an application, your operating system loads the application into some of that memory. This memory used by your application is divided into different areas, each serving a different purpose. One area contains your code. Another area is used for normal operations (keeping track of which functions were called, creating and destroying global and local variables, etc.). We'll discuss those later. However, much of the memory available just sits there, waiting to be handed out to programs that request it.
When you dynamically allocate memory, you're asking the operating system to reserve some of that memory for your program's use. If it can fulfill this request, it will return the address of that memory to your application. From that point forward, your application can use this memory as it wishes. When your application is done with the memory, it can return the memory back to the operating system to be given to another program.
Unlike static or automatic memory, the program itself is responsible for requesting and disposing of dynamically allocated memory.
The allocation and deallocation for stack objects is done automatically. There is no need for us to deal with memory addresses -- the code the compiler writes can do this for us.
The allocation and deallocation for heap objects is not done automatically. We need to be involved. That means we need some unambiguous way to refer to a specific heap allocated object, so that we can request its destruction when we're ready. And the way we reference such objects is via memory address.
When we use operator new, it returns a pointer containing the memory address of the newly allocated object. We generally want to store that in a pointer so we can use that address later to access the object (and eventually, request its destruction).
When you dynamically allocate a variable, you can also initialize it via direct initialization or uniform initialization:
int* score1{ new int(87) };
int* score2{ new int{ 91 } };
Deleting a single variable
When we are done with a dynamically allocated variable, we need to explicitly tell C++ to free the memory for reuse. For single variables, this is done via the scalar (non-array) form of the delete operator:
delete ptr;
ptr = nullptr;
What does it mean to delete memory?
The delete operator does not actually delete anything. It simply returns the memory being pointed to back to the operating system. The operating system is then free to reassign that memory to another application (or to this application again later).
Although the syntax makes it look like we're deleting a variable, this is not the case! The pointer variable still has the same scope as before, and can be assigned a new value (e.g., nullptr) just like any other variable.
Note that deleting a pointer that is not pointing to dynamically allocated memory may cause bad things to happen.
Dangling pointers
C++ does not make any guarantees about what will happen to the contents of deallocated memory, or to the value of the pointer being deleted. In most cases, the memory returned to the operating system will contain the same values it had before it was returned, and the pointer will be left pointing to the now deallocated memory.
A pointer that is pointing to deallocated memory is called a dangling pointer. Dereferencing or deleting a dangling pointer will lead to undefined behavior. Consider the following program:
#include <iostream>
int main()
{
int* score{ new int };
*score = 95;
delete score;
std::cout << *score;
delete score;
return 0;
}
In the above program, the value of 95 that was previously assigned to the allocated memory will probably still be there, but it's possible that the value at that memory address could have changed. It's also possible the memory could be allocated to another application (or for the operating system's own usage), and trying to access that memory will cause the operating system to shut the program down.
Deallocating memory may create multiple dangling pointers. Consider the following example:
#include <iostream>
int main()
{
int* temperature{ new int{} };
int* otherTemp{ temperature };
delete temperature;
temperature = nullptr;
return 0;
}
There are a few best practices that can help here.
First, try to avoid having multiple pointers point at the same piece of dynamic memory. If this is not possible, be clear about which pointer "owns" the memory (and is responsible for deleting it) and which pointers are just accessing it.
Second, when you delete a pointer, if that pointer is not going out of scope immediately afterward, set the pointer to nullptr. We'll talk more about null pointers, and why they are useful in a bit.
Set deleted pointers to nullptr unless they are going out of scope immediately afterward.
When requesting memory from the operating system, in rare circumstances, the operating system may not have any memory to grant the request with.
By default, if new fails, a bad_alloc exception is thrown. If this exception isn't properly handled (and it won't be, since we haven't covered exceptions or exception handling yet), the program will simply terminate (crash) with an unhandled exception error.
In many cases, having new throw an exception (or having your program crash) is undesirable, so there's an alternate form of new that can be used instead to tell new to return a null pointer if memory can't be allocated. This is done by adding the constant std::nothrow between the new keyword and the allocation type:
int* score{ new (std::nothrow) int };
In the above example, if new fails to allocate memory, it will return a null pointer instead of the address of the allocated memory.
Note that if you then attempt to dereference this pointer, undefined behavior will result (most likely, your program will crash). Consequently, the best practice is to check all memory requests to ensure they actually succeeded before using the allocated memory.
int* score{ new (std::nothrow) int{} };
if (!score)
{
std::cerr << "Could not allocate memory\n";
}
Because asking new for memory only fails rarely (and almost never in a dev environment), it's common to forget to do this check!
Null pointers and dynamic memory allocation
Null pointers (pointers set to nullptr) are particularly useful when dealing with dynamic memory allocation. In the context of dynamic memory allocation, a null pointer basically says "no memory has been allocated to this pointer". This allows us to do things like conditionally allocate memory:
if (!ptr)
ptr = new int;
Deleting a null pointer has no effect. Thus, there is no need for the following:
if (ptr)
delete ptr;
Instead, you can just write:
delete ptr;
If ptr is non-null, the dynamically allocated memory will be deleted. If ptr is null, nothing will happen.
Deleting a null pointer is okay, and does nothing. There is no need to conditionalize your delete statements.
Dynamically allocated memory stays allocated until it is explicitly deallocated or until the program ends (and the operating system cleans it up, assuming your operating system does that). However, the pointers used to hold dynamically allocated memory addresses follow the normal scoping rules for local variables. This mismatch can create interesting problems.
Consider the following function:
void processData()
{
int* measurement{ new int{} };
}
This function allocates an integer dynamically, but never frees it using delete. Because pointers variables are just normal variables, when the function ends, measurement will go out of scope. And because measurement is the only variable holding the address of the dynamically allocated integer, when measurement is destroyed there are no more references to the dynamically allocated memory. This means the program has now "lost" the address of the dynamically allocated memory. As a result, this dynamically allocated integer can not be deleted.
This is called a memory leak. Memory leaks happen when your program loses the address of some bit of dynamically allocated memory before giving it back to the operating system. When this happens, your program can't delete the dynamically allocated memory, because it no longer knows where it is. The operating system also can't use this memory, because that memory is considered to be still in use by your program.
Memory leaks eat up free memory while the program is running, making less memory available not only to this program, but to other programs as well. Programs with severe memory leak problems can eat all the available memory, causing the entire machine to run slowly or even crash. Only after your program terminates is the operating system able to clean up and "reclaim" all leaked memory.
Although memory leaks can result from a pointer going out of scope, there are other ways that memory leaks can result. For example, a memory leak can occur if a pointer holding the address of the dynamically allocated memory is assigned another value:
int threshold{ 100 };
int* limit{ new int{} };
limit = &threshold;
This can be fixed by deleting the pointer before reassigning it:
int threshold{ 100 };
int* limit{ new int{} };
delete limit;
limit = &threshold;
Relatedly, it is also possible to get a memory leak via double-allocation:
int* limit{ new int{} };
limit = new int{};
The address returned from the second allocation overwrites the address of the first allocation. Consequently, the first allocation becomes a memory leak!
Similarly, this can be avoided by ensuring you delete the pointer before reassigning.
Conclusion
Operators new and delete allow us to dynamically allocate single variables for our programs.
Dynamically allocated memory has dynamic duration and will stay allocated until you deallocate it or the program terminates.
Be careful not to dereference a dangling or null pointer.
In the next lesson, we'll take a look at using new and delete to allocate and delete arrays.
Summary
Three types of memory allocation: Static (global/static variables, allocated at program start), automatic (function parameters/local variables, allocated when entering a block), and dynamic (allocated on demand from the heap).
Limitations of static/automatic allocation: Both require compile-time known sizes and are automatically managed. This creates problems when sizes are determined at runtime or when large amounts of memory are needed.
The stack is limited in size (typically 1MB), making it unsuitable for large arrays or memory-intensive structures. Exceeding stack limits causes stack overflow and program termination.
The heap is a much larger pool of memory (potentially gigabytes) managed by the operating system from which programs can request memory at runtime.
Dynamic memory allocation allows programs to request memory from the heap at runtime using the new operator. The new operator allocates memory, constructs the object, and returns a pointer to the allocated memory.
The delete operator returns dynamically allocated memory back to the operating system for reuse. It does not delete the pointer variable itself, only returns the memory pointed to.
Dangling pointers point to deallocated memory. Dereferencing or deleting a dangling pointer causes undefined behavior. Set deleted pointers to nullptr unless they immediately go out of scope.
Null pointers (pointers set to nullptr) indicate no memory has been allocated. Deleting a null pointer is safe and does nothing, so conditional deletion is unnecessary.
operator new can fail: When memory allocation fails, new throws a bad_alloc exception by default. Use new (std::nothrow) to return nullptr instead. Always check the result when using nothrow.
Memory leaks occur when dynamically allocated memory loses all references before being deleted, making it impossible to free. Common causes include pointers going out of scope, pointer reassignment without deletion, and double-allocation.
Heap vs stack comparison:
- Heap: Slower allocation, persists until explicitly freed, accessed via pointers (slower), can be very large
- Stack: Fast allocation, freed automatically, direct access (faster), limited size
Dynamic memory allocation provides flexibility for runtime-sized data and large memory needs, but requires careful management to avoid memory leaks and dangling pointers.
Manual Memory Management with new and delete - Quiz
Test your understanding of the lesson.
Practice Exercises
Dynamic Memory Basics
Practice allocating and deallocating single variables on the heap using new and delete. Demonstrate proper memory management patterns.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!