What is the Difference Between new and delete Operator in C++

The main difference between new and delete operator in C++ is that new is used to allocate memory for an object or an array while, delete is used to deallocate the memory allocated using the new operator.

There are two types of memory as static and dynamic memory. Assume a statement such as int a; int array[10]; the programmer already knows how much memory these variables require. This memory is a static memory. Sometimes it is difficult to determine how much memory it takes to store data. Therefore, the memory should be determined at runtime. This type of memory is a dynamic memory. We use new and delete operators in these situations.

Key Areas Covered

1. What is new Operator in C++
     – Definition, Functionality
2. What is delete Operator in C++
     – Definition, Functionality
3. What is the Difference Between new and delete Operator in C++
     – Comparison of Key Differences

Key Terms

C++, delete Operator, Memory Allocation, new Operator

Difference Between new and delete Operator - Comparison Summary

What is new in C++

The new operator allocates memory. Refer the below program.

Difference Between new and delete Operator in C++

Figure 1: C++ program 1 with new operator

According to the above program, the ptr is an integer type pointer. In line 7, ptr points to int type memory. In line 8, the value ptr pointing to become 10. Printing *p will give the value 10.

Similarly, it is possible to create arrays using new as follows.

Main Difference - new vs delete Operator in C++

Figure 2:  C++ program 2 with new operator

According to the above program, ptr is an integer type pointer. In line 8, ptr points to the memory of int type which can store 5 integer elements. Then, the integer values are stored in the array. Finally, for loop iterates through the array and will print the values.

Furthermore, it is possible to create new objects using the new operator. Refer the below example.

Difference Between new and delete Operator in C++_Figure 3

Figure 3: C++ program 3 with new operator

The class is Student, and it has the constructor. In the main program, ptr is a Student type pointer. In line 13, the constructor is called. Therefore, a Student type object is created. The cout statement inside the Student constructor executes, and that statement is displayed on the console.

What is delete in C++

The delete operator helps to deallocate memory. Refer the below program.

Difference Between new and delete Operator in C++_Figure 4

Figure 4: C++ program 1 with delete operator

In the above program, ptr is an int type pointer. In line 7, it points to int type memory while in line 8, it is given the value 10. Moreover, in line 11, ptr is deleted.

Difference Between new and delete Operator in C++_Figure 5

Figure 5: C++ program 2 with delete operator

According to the above program, ptr is an integer type pointer. In line 8, ptr points to the memory of int type, which can store 2 integer elements. Then, the integer values are stored in the array. In line 12, ptr is deleted. The delete operator deallocates the memory.

Difference Between new and delete Operator in C++

Definition

In brief, new is an operator in C++ that allocates memory for an object or an array of objects. In contrast, delete is an operator in C++ that deallocates a block of memory previously allocated for an object created using the new operator. These definitions contain the main difference between new and delete operator in C++.

Usage

Moreover, new operator helps to allocate memory for an array or an object whereas delete operator helps to deallocate the allocated memory to a particular object or an array. Hence, this is another difference between new and delete operator in C++.

Conclusion

The basic difference between new and delete operator in C++ is that new is used to allocate memory for an object or an array while delete is used to deallocate the memory allocated using the new operator.

Reference:

1. New OPERATOR IN C – 47, Codearchery, 6 Mar. 2017, Available here.
2. Delete Operator in C – 48, Codearchery, 22 Mar. 2017, Available here.

About the Author: Lithmee

Lithmee holds a Bachelor of Science degree in Computer Systems Engineering and is reading for her Master’s degree in Computer Science. She is passionate about sharing her knowldge in the areas of programming, data science, and computer systems.

Leave a Reply