Dynamic Memory Allocation in C++ Programming Language: Managing Memory on the Go
Introduction of dynamic memory in C++ is indispensable for achieving mastery in the language. Dynamic Memory Management in C++ allo
ws programmers to allocate and deallocate memory during runtime, enabling flexible memory usage for varying program needs. Memory in a C++ program is partitioned into two main areas:- The Stack: This region is utilized for storing variables declared within functions.
- The Heap: The heap represents unused memory in the program that can be employed to allocate memory dynamically as the program runs.
Often, you’re not aware in advance of the exact amount of memory required to store specific information in a variable. The size of the necessary memory can only be determined at runtime.
C++ addresses this by permitting you to allocate memory at runtime within the heap for variables of various types. The key operator facilitating this is the new
operator. This operator returns the address of the allocated space, enabling you to manage memory as needed.
When the dynamically allocated memory is no longer needed, you can employ the delete
operator to deallocate the memory allocated by the new
operator.
The new and delete Operators
The new operator adheres to the following generic syntax for dynamically allocating memory for any data type:
new data-type;
Here, data-type
could be any built-in data type, including an array, or user-defined data types like classes or structures.
For example, consider the allocation of memory for a double
variable using the new operator:
double* pvalue = NULL; // Pointer initialized with null
pvalue = new double; // Request memory for the variable
However, memory allocation may fail if the free store (heap) is exhausted. Therefore, it’s good practice to check if the new operator returns a NULL pointer and take appropriate action.
double* pvalue = NULL;
if (!(pvalue = new double)) {
cout << "Error: out of memory." << endl;
exit(1);
}
It’s important to note that although the malloc()
function from C still exists in C++, the use of new
is preferred. Unlike malloc()
, new
doesn’t merely allocate memory; it also constructs objects, aligning with the core principles of C++.
To free memory allocated by the new
operator when it’s no longer needed, you can use the delete
operator as follows:
delete pvalue; // Release memory pointed to by pvalue
Dynamic Memory Allocation for Arrays
You can allocate memory for arrays dynamically using the new
operator. For example, allocating memory for a string of 20 characters:
char* pvalue = NULL; // Pointer initialized with null
pvalue = new char[20]; // Request memory for the variable
To release the array you’ve created, use the delete
operator as follows:
delete [] pvalue; // Delete array pointed to by pvalue
Similarly, dynamic memory allocation for multi-dimensional arrays is feasible:
double** pvalue = NULL; // Pointer initialized with null
pvalue = new double[3][4]; // Allocate memory for a 3x4 array
As before, the syntax to release memory for a multi-dimensional array remains consistent:
delete [] pvalue; // Delete array pointed to by pvalue
Dynamic Memory Allocation for Objects
Objects are treated no differently from simple data types. Consider the following example using an array of objects:
#include <iostream>
using namespace std;
class Box {
public:
Box() {
cout << "Constructor called!" << endl;
}
~Box() {
cout << "Destructor called!" << endl;
}
};
int main() {
Box* myBoxArray = new Box[4];
delete [] myBoxArray; // Delete array
return 0;
}
Here, the constructor is called when objects are allocated, and the destructor is called when the objects are deleted. The usage of the new
and delete
operators for objects follows the same principles as for other data types.
Understanding dynamic memory allocation in C++ is a foundational skill that empowers you to manage memory efficiently as your program runs. The strategic use of new
and delete
helps you optimize memory usage and build robust applications.
Discover more from PiEmbSysTech
Subscribe to get the latest posts sent to your email.