Dynamic memory allocation and deallocation in C++ involves using the new and delete operators. These operators allow you to allocate memory for objects on the heap during runtime and deallocate that memory when it’s no longer needed. The new operator is used for memory allocation, and the delete operator is used for memory deallocation.

Dynamic Memory Allocation using new:

The new operator is used to allocate memory for a single object or an array of objects on the heap. The syntax for using new is as follows:

type *pointer = new type;        // For a single object
type *arrayPointer = new type[n]; // For an array of objects

Here, type is the data type of the object(s) you want to allocate memory for, and n is the number of elements in the array.

Example for a single object:

int *intPointer = new int; // Allocates memory for a single integer
*intPointer = 42;          // Assign a value

Example for an array of objects:

For dynamically allocated arrays, you can use new[] to allocate memory for an array.

double *doubleArray = new double[5]; // Allocates memory for an array of 5 double values
for (int i = 0; i < 5; ++i) {
    doubleArray[i] = i * 1.5;
}

Dynamic Memory Deallocation using delete:

The delete operator is used to release the memory that was allocated using the new operator. It is important to deallocate memory to prevent memory leaks. The syntax for using delete is as follows:

delete pointer;         // For a single object
delete[] arrayPointer;  // For an array of objects

Example for a single object:

delete intPointer; // Deallocate memory for the integer

Example for an array of objects:

For dynamically allocated arrays, you can use delete[] to deallocate memory for an array

delete[] doubleArray; // Deallocate memory for the array of doubles

It’s important to note that forgetting to deallocate memory can lead to memory leaks, where memory is allocated but not released, causing your program to consume more and more memory over time.

Important Considerations:

  • Always pair new with delete, and new[] with delete[] to avoid memory leaks.
  • After calling delete or delete[], the memory is not automatically set to nullptr, so it’s a good practice to set the pointer to nullptr after deallocation.
  • Accessing memory after it has been deallocated results in undefined behavior.
  • Improper memory deallocation can lead to memory leaks or program crashes.

Example of dynamic memory allocation and deallocation using new and delete-

In C++, the new operator is used to dynamically allocate memory for an object on the heap, and the delete operator is used to deallocate that memory when it’s no longer needed. Here’s an example:

#include <iostream>

class MyClass {
public:
    MyClass(int val) : value(val) {
        std::cout << "Constructor called with value: " << value << std::endl;
    }

    ~MyClass() {
        std::cout << "Destructor called for value: " << value << std::endl;
    }

    void printValue() {
        std::cout << "Value: " << value << std::endl;
    }

private:
    int value;
};

int main() {
    // Using 'new' to dynamically allocate memory for an object
    MyClass *objPtr = new MyClass(42);

    // Accessing member function of the dynamically allocated object
    objPtr->printValue();

    // Deallocating memory using 'delete'
    delete objPtr;

    return 0;
}

In this example:

  1. We define a class MyClass with a constructor, destructor, and a member function to print the value.
  2. In the main() function, we use the new operator to dynamically allocate memory for an instance of MyClass with a value of 42.
  3. We access the member function of the dynamically allocated object using the pointer (objPtr->printValue()).
  4. Finally, we use the delete operator to deallocate the memory for the object, which triggers the destructor of the class.

Note that in modern C++ programming, it’s recommended to use smart pointers (std::unique_ptr and std::shared_ptr) and RAII (Resource Acquisition Is Initialization) principles to manage memory and resources, which helps avoid many common memory-related issues like memory leaks.


more related content on Object Oriented Programming

JOIN OUR NEWSLETTER
And get notified everytime we publish a new blog post.