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
What is new in C++
The new operator allocates memory. Refer the below program.
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.
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.
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.
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.
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.
Leave a Reply