Article by Ayman Alheraki on January 11 2026 10:32 AM
In C++, memory allocation is a fundamental part of memory management. Developers must make conscious decisions about how to allocate and deallocate memory to ensure that programs run efficiently and securely. In this chapter, we will explore static and dynamic memory allocation mechanisms, discuss the use of operators new, delete, new[], and delete[], and highlight common memory-related issues such as memory leaks, use-after-free, and uninitialized pointers.
Static Memory Allocation:
Static memory allocation occurs when memory is allocated for variables at compile time. Static variables are typically defined at the global scope or using the static keyword. These variables remain in memory for the entire duration of the program and cannot be resized or deallocated during execution.
Example:
xxxxxxxxxxstatic int number = 10; // Static allocationint globalVar = 20; // Static memory allocationDynamic Memory Allocation:
In contrast to static allocation, dynamic memory allocation occurs at runtime using the new operator. Memory is allocated from the heap, which is flexible and can be allocated and deallocated as needed. This allows developers to create data structures with sizes that are unknown or variable at compile time.
Example:
xxxxxxxxxxint* ptr = new int; // Dynamic allocation for a single objectint* arr = new int[10]; // Dynamic allocation for an arraynew, delete, new[], and delete[]new and delete:
new: The new operator is used to allocate memory for a single object or an array of objects on the heap. When using new, memory should be deallocated using delete to avoid memory leaks.
Example:
xxxxxxxxxxint* ptr = new int; // Allocate memory for an int*ptr = 5; // Assign value to the objectdelete ptr; // Deallocate memory to avoid leaksdelete: The delete operator is used to deallocate memory that was previously allocated for a single object using new. Failure to use delete can result in memory leaks.
new[] and delete[]:
new[]: Used to allocate memory for an array of objects. delete[] should be used to deallocate this memory.
Example:
xxxxxxxxxxint* arr = new int[10]; // Allocate an array of 10 elementsdelete[] arr; // Deallocate memory for the arraydelete[]: delete[] should be used with new[] to deallocate memory allocated for an array of objects. Failing to do so can lead to undefined behavior.
A. Memory Leaks:
Memory leaks occur when memory is allocated but not deallocated after use, leading to the program losing track of that memory and being unable to reuse it. This can result in excessive memory consumption and degraded program performance, and eventually lead to system crashes.
Example:
xxxxxxxxxxvoid func() { int* data = new int[100]; // Allocate memory for an array // Forget to deallocate memory with delete[] // delete[] data; // Memory leak}B. Use-After-Free:
This issue occurs when an attempt is made to access memory after it has been deallocated using delete or delete[]. This can lead to undefined behavior or even security vulnerabilities.
Example:
xxxxxxxxxxint* ptr = new int;delete ptr; // Deallocate memory*ptr = 10; // Unsafe use of memory after deallocationC. Uninitialized Pointers:
This issue arises when pointers that have not been properly initialized are used to reference a memory location. This can lead to undefined behavior, such as program crashes or accessing incorrect data.
Example:
xxxxxxxxxxint* ptr; // Uninitialized pointer*ptr = 5; // Undefined behaviorIn this article, we discussed the mechanisms of memory allocation in C++, including static and dynamic allocation. We also covered the use of new, delete, new[], and delete[], and highlighted common issues related to memory management such as memory leaks, use-after-free, and uninitialized pointers. By understanding these mechanisms and issues, developers can write more secure and efficient code in C++.