Dynamic Memory Allocation in C++: New and Delete

Explore practical examples of dynamic memory allocation in C++ using 'new' and 'delete'. Learn how to manage memory effectively.
By Jamie

Dynamic memory allocation in C++ allows programmers to request and manage memory at runtime, enabling the creation of flexible and efficient applications. The new operator allocates memory on the heap, while the delete operator releases it, preventing memory leaks. Here are three practical examples demonstrating these concepts.

Example 1: Allocating a Simple Integer Array

Context

Dynamic allocation of arrays is useful when the size of the array is not known at compile time. This example shows how to allocate an integer array and initialize its values.

#include <iostream>
using namespace std;

int main() {
    int size;
    cout << "Enter the size of the array: ";
    cin >> size;

    // Dynamic memory allocation for an array
    int* arr = new int[size];

    // Initializing the array
    for (int i = 0; i < size; i++) {
        arr[i] = i * 10;
    }

    // Displaying the array elements
    cout << "Array elements: ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    // Deallocating memory
    delete[] arr;
    return 0;
}

Notes

  • This example demonstrates the use of new to allocate an array and delete[] to free the memory.
  • Always ensure that delete[] is used for arrays allocated with new[] to avoid undefined behavior.

Example 2: Creating a Dynamic Object

Context

When working with classes, dynamic memory allocation enables the creation of objects whose lifetimes can be controlled. This example illustrates how to create a dynamic object of a class.

#include <iostream>
using namespace std;

class Point {
public:
    int x, y;
    Point(int xCoord, int yCoord) : x(xCoord), y(yCoord) {}
    void display() {
        cout << "Point(" << x << ", " << y << ")" << endl;
    }
};

int main() {
    // Dynamic memory allocation for a Point object
    Point* p = new Point(10, 20);

    // Using the object
    p->display();

    // Deallocating memory
    delete p;
    return 0;
}

Notes

  • The new operator allocates memory for a Point object, and delete is used to free that memory.
  • Using pointers for objects allows for more flexible memory management, especially in large-scale applications.

Example 3: Dynamic 2D Array Allocation

Context

Dynamic 2D arrays are frequently used in matrix operations or grid-based applications. This example shows how to create and manage a dynamic 2D array.

#include <iostream>
using namespace std;

int main() {
    int rows, cols;
    cout << "Enter number of rows and columns: ";
    cin >> rows >> cols;

    // Dynamic memory allocation for a 2D array
    int** matrix = new int*[rows];
    for (int i = 0; i < rows; i++) {
        matrix[i] = new int[cols];
    }

    // Initializing the 2D array
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            matrix[i][j] = i * j;
        }
    }

    // Displaying the 2D array
    cout << "Matrix:" << endl;
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < cols; j++) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }

    // Deallocating memory
    for (int i = 0; i < rows; i++) {
        delete[] matrix[i];
    }
    delete[] matrix;
    return 0;
}

Notes

  • This example demonstrates how to allocate a 2D array using an array of pointers.
  • Each row is allocated individually, and delete[] is used for both rows and the array of pointers to prevent memory leaks.

These examples provide a solid foundation for understanding dynamic memory allocation in C++. By mastering the use of new and delete, you can create flexible and efficient C++ applications.