Memory Management in C++

C++ Memory Management

In C++, memory management is typically done through the use of pointers and the new and delete operators. The new operator is used to dynamically allocate memory for a variable or an array of variables at runtime, and it returns a pointer to the start of the allocated memory. The delete operator is used to deallocate memory that was previously allocated by new. Let us learn everything about Memory Management in C++ on this page.

Memory Management in C++

Memory Management : C++

When memory is allocated dynamically and not deallocated, it leads to memory leaks, which can cause your program to run out of memory and crash.

C++11 also introduce smart pointers, like std::unique_ptr, std::shared_ptr and std::weak_ptr which helps managing memory by handling the deletion automatically, reducing the risk of memory leaks and dangling pointers.

It is important to use delete and delete[] correctly to avoid undefined behavior and memory leaks, when you are allocating memory using new and new[].

Additionally, C++ also provides the new and delete operators with matching placement versions, new (p) T and delete (p), that allows to use a pre-allocated buffer to create objects, this is called placement new and placement delete.

C++ new Operator

In C++, the new operator is used to dynamically allocate memory for a variable or an array of variables at runtime. The operator returns a pointer to the start of the allocated memory, which can then be used to access the memory.

The basic syntax for using the new operator to allocate memory for a single variable is:

Type* ptr = new Type;

Here Type is the data type of the variable, and ptr is a pointer that will store the address of the allocated memory.

You can also use the new operator to allocate memory for an array of variables:

Type* ptr = new Type[size];

Here Type is the data type of the array elements, size is the number of elements in the array, and ptr is a pointer that will store the address of the allocated memory.

C++ delete Operator

In C++, the delete operator is used to deallocate memory that was previously allocated by the new operator. When you no longer need the memory, it should be deallocated to prevent memory leaks.

Once you’re done using the memory allocated with the new operator, you should release the memory using the delete operator:

delete ptr;

Or when allocating an array

delete[] ptr;

It is important to match the delete operator with the allocation method, using delete with memory allocated with new[] and vice versa will cause undefined behavior.

C++ also provides the new operator with matching placement versions, new (p) T and delete (p) which allows to use preallocated buffer to create objects, this is called placement new and placement delete.

C++ new and delete Operator for Arrays

Run
#include <iostream>
using namespace std;

int main() {

int num;
cout << "Enter total number of aspirants: ";
cin >> num;
float* ptr;

// memory allocation of num number of floats
ptr = new float[num];

cout << "Enter marks of aspirants." << endl;
for (int i = 0; i < num; ++i) {
cout << "aspirant" << i + 1 << ": ";
cin >> *(ptr + i);
}

cout << "\nDisplaying marks of aspirants." << endl;
for (int i = 0; i < num; ++i) {
cout << "aspirant" << i + 1 << ": " << *(ptr + i) << endl;
}

// ptr memory is released
delete[] ptr;

return 0;
}

Output

Enter total number of aspirants: 2
Enter marks of aspirants.
aspirant1: 87
aspirant2: 75

Displaying marks of aspirants.
aspirant1: 87
aspirant2: 75

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription