Difference Between New And Delete Operator In C++

In the C++ programming language, the new and delete operators are mainly utilized for dynamic memory allocation and deallocation. They enable us to dynamically allocate and free the memory, which means that we can create objects whose size and lifetime may be defined at runtime.

In this article, we will discuss the difference between new and delete operators in C++. Before discussing their differences, we must know about the new and delete operators in C++.

What is the new Operator in C++?

In C++, the new operator is a memory allocation operator that is utilized to allocate the memory at the runtime. The new operator allocates the memory initialized in a heap. It returns the beginning address of the allocated memory that can be stored in a pointer variable. It allows us to allocate memory when the size or number of required variables is not known at compile time.

Syntax

It has the following syntax:

Example

type variable = new type(parameter_list);

In this syntax,

  • type: It represents the datatype of the variable for which the memory is allocated by the new operator.
  • new: It is an operator that is used to allocate the memory.
  • variable: It is the name of the variable thacpp tutorials to the memory.
  • parameter_list: It is the list of values that are initialized to a variable.
  • C++ new Operator Example

Let us take an example to illustrate the new operator in C++.

Example

Example

#include <iostream>

using namespace std;  //using standard namespace

int main() {  //main function

    // Dynamically allocate memory

    int* ptr = new int;

    // Assign a value

    *ptr = 15;

    // Print the value

    cout << "The value stored at ptr is: " << *ptr << endl;

    return 0;

}

Output:

Output

The value stored at ptr is: 15

Explanation:

In this example, we demonstrate the new operator in C++. First, we have taken a new int to allocate memory for one integer. After that, the *ptr = 15 stores the value in the allocated memory. Finally, it prints the output.

What is the delete Operator in C++?

The delete operator is an operator that is used in C++ programming language , and it is used to deallocate the memory dynamically. This operator is mainly used for those pointers that are allocated using a new operator or NULL pointer.

Syntax

It has the following syntax:

Example

delete pointer_variable;   // For single variables

delete[] pointer_variable;

C++ delete Operator Example

Let us take an example to illustrate the delete operator in C++.

Example

Example

#include <iostream>

using namespace std;  //using standard namespace

int main() {   //main function

    int* arr = new int[6];  // Dynamically allocate an array

    for (int i = 0; i < 6; i++) {

        arr[i] = i + 1;

    }

    cout << "Array elements are : ";

    for (int i = 0; i < 6; i++) {

        cout << arr[i] << " ";

    }

    delete[] arr;  // Deallocate array memory

    return 0;

}

Output:

Output

Array elements are : 1 2 3 4 5 6

Key Differences between new and delete Operators in C++

There are several main differences between the new and delete operators in C++. Some main differences are as follows:

Definition

The new operator is mainly utilized to allocate memory for an object or an array of objects. It returns a pointer to the allocated memory. In contrast, the delete operator is mainly utilized to deallocate memory that was previously allocated with new. It releases the memory pointed to by the pointer and sets the pointer to null.

Syntax

The syntax for the new operator is:

Example

pointer_variable = new data_type;

The syntax for the delete operator is:

Example

delete pointer_variable;

Allocation of memory

In C++, the new operator allocates memory for the object or array of objects from the heap, which is a global pool of memory. The memory block size is specified by the object data type or array of objects. In contrast, the delete operator frees the memory allocated by the new operator.

Initialization

In C++, the new operator initializes the memory that is allocated for an object with the default class constructor. If there is no default constructor, the placement new operator can be utilized to call the suitable constructor. In contrast, the delete operator does not perform any initialization. It just calls the destructor of objects and deallocates the memory.

Allocation of array

In C++, the new operator can be mainly utilized to allocate memory for an array of objects.

Syntax

The syntax for array allocation is:

Example

pointer_variable = new data_type[array_size];

On the other hand, the delete operator can be utilized to deallocate an array of objects allocated with the new operator.

Syntax

The syntax for array deallocation is:

Example

delete[] pointer_variable;

Exception Handling

In C++, if the new operator fails to allocate memory, it can throw an exception. In this case, the program may catch the exception and take suitable action. In conctrast, the delete operator does not throw any exceptions.

Operator Overloading

In C++, both the new and delete operators can be overloaded, which allows us to customize how dynamic memory is allocated and deallocated for specific classes. It is especially helpful for several cases, such as debugging, memory pooling, and implementing custom memory management systems.

Global Overload

In C++, we can overload new and delete operators globally to change the behavior of memory allocation and deallocation in the program.

New Operator Syntax

The syntax for overloading the global new operator are as follows:

Example

void* operator new(size_t size);

Delete Operator Syntax

The syntax for overloading the global delete operator are as follows:

Example

void operator delete(void* ptr);

By overloading these operators, it allows us to customize the memory allocation and deallocation behavior in the program. It can be very helpful in several cases, such as developing real-time systems and embedded systems.

Difference between new and delete Operators in Tabular Form

Several main differences between new and delete operators in C++ is given below:

Features New Operator Delete Operator
Definition It allocates memory for an object or array of objects. It deallocates memory allocated by new.
Syntax pointervariable = new datatype; delete pointer_variable;
Memory Allocation It allocates memory from the heap. It frees memory allocated by the new operator.
Initialization It initializes memory with the default constructor. It does not perform any initialization process.
Array Allocation It allocates memory for an array of objects. It deallocates an array of objects.
Exception Handling It can throw an exception if it fails to allocate memory. It does not throw any exceptions.

Conclusion

In conclusion, the new and delete operators are important features of dynamic memory management. Both new and delete operators perform tasks on the heap memory, but they also have several differences, such as syntax, usage, and initialization behavior. By understanding these operators, we can utilize these operators to prevent common issues, such as memory leaks and provide better control over system resources.

Difference between New and Delete Operator FAQs

1) What is the new operator in C++?

The new operator dynamically allocates the memory on the heap for objects or arrays of objects.

2) What is the delete operator in C++?

The delete operator deallocates the memory that the new operator allocated for a single object or variable.

3) What happens if we call the delete operator twice on the same pointer?

If we call the delete operator multiple times on the same pointer, it leads to undefined behavior and program crashes.

4) Can new and delete operators be overloaded in C++?

Yes, we can overload new and delete operators in a class to customize the memory allocation and deallocation.

5) What is the difference between the delete and delete in C++?

The delete operator is mainly utilized to deallocate the memory for a single object. In contrast, the delete is mainly utilized to deallocate the memory for an array of objects that are allocated using new.

Input Required

This code uses input(). Please provide values below: