C++ New & Delete

Usage of 'new' & 'delete' operator in C++.

* The 'new' and 'delete' are not considered "old" or obsolete in C++, they are commonly referred to as manual memory management.

New & Delete

In C++, 'new' and 'delete' are operators used for dynamic memory allocation and deallocation, respectively. They are part of the dynamic memory management features provided by the language.

1. new Operator:

  • The 'new' operator is used to dynamically allocate memory for a single variable or an array of variables on the heap.
  • It returns a pointer to the allocated memory.
  • The syntax for allocating a single variable is 'new Type', and for an array, it is 'new Type[size]'.
  • It is often used with a pointer to store the address of the dynamically allocated memory.

Syntax:

int* dynamicVar = new int;
int* dynamicArray = new int[5];

Line 1: Dynamically allocate memory for a single integer.
Line 2: Dynamically allocate memory for an array of 5 integers.

2. delete Operator:

  • The 'delete' operator is used to free the memory that was previously allocated using 'new'.
  • The syntax for deleting a single variable is 'delete pointer', and for deleting an array, it is 'delete[] pointer'.
  • It is crucial to use the correct form of 'delete' corresponding to how the memory was allocated with 'new'.

Syntax:

delete dynamicVar;
delete[] dynamicArray;

Line 1:Deallocate memory for a single integer.
Line 2: Deallocate memory for an array of integers.

Note: Failure to use the correct form of 'delete' can lead to undefined behavior and memory leaks.

Manual memory management with 'new' and 'delete' requires careful handling to avoid memory leaks (not deallocating memory) and dangling pointers (using a pointer after the memory it points to has been deallocated). To simplify memory management and reduce the risk of such issues, C++11 introduced smart pointers, such as 'std::unique_ptr' and 'std::shared_ptr', which automatically manage memory and deallocate it when it's no longer needed. Using smart pointers is generally recommended over manual memory management when possible.

Can we use the malloc(), calloc(), realloc(), and free() in C++?

Yes, these functions are part of the C standard library, and they can be used in C++ as well. These functions also provide a way to perform dynamic memory allocation and deallocation.

Now, we'll dynamically allocate an array of integers and then deallocate the memory to prevent memory leaks:

cpp Copy Code
#include<iostream>

int main() {
    // Dynamic memory allocation for an array of integers
    int size;
    std::cout << "Enter the size of the array: ";
    std::cin >> size;

    // Allocate memory for an array of integers
    int *dynamicArray = new int[size];

    // Initialize the array elements
    for (int i = 0; i < size; ++i) {
        dynamicArray[i] = i * 10; // Just an example initialization
    }

    // Display the contents of the dynamically allocated array
    std::cout << "Dynamically allocated array elements: ";
    for (int i = 0; i < size; ++i) {
        std::cout << dynamicArray[i] << " ";
    }
    std::cout << std::endl;

    // Deallocate the dynamically allocated memory
    delete[] dynamicArray;

    return 0;
}
Output:
Enter the size of the array: 5
Dynamically allocated array elements: 0 10 20 30 40

Remember: Use 'delete[]' when deallocating memory for an array, and 'delete' when deallocating memory for a single variable.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.