What’s diff btw new operator and delete operator in C++?

                       The main difference between new operator and delete in C++ is that new is used to allocate memory to an object or an array while delete is used to deallocate memory allocated using the new operator . What’s diff btw new operator and delete operator in C++?

There are two types of memory as static and dynamic memory. Suppose a statement like 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 is needed to store data. Therefore, the memory must be determined at run time. This type of memory is a dynamic memory. We use new and removed 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++
     – Key differences comparison

What’s new in C++ What’s diff btw new operator and delete operator in C++?

The new operator allocates memory. See the following program.

According to the above program, ptr is a pointer of type integer. On line 7, ptr points to memory of type int. On line 8, the value ptr pointing to become 10. Print *p will give the value 10.

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

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

Also, it is possible to create new objects using the new operator. See the example below.

The class is student, and it has the constructor. In the main program, ptr is a student type pointer. On line 13, the constructor is called. Therefore, an object of type Student is created. The cout statement inside the Student constructor is executed, and that statement is displayed to the console.

What is delete in C++

The delete operator helps to deallocate memory. See the following program.

In the above program, ptr is a pointer of type int. On line 7, it points to memory of type int, while on line 8, it is assigned the value 10. Also, on line 11, ptr is removed.

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

Difference between new and delete operator in C++

Definition

In short, 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 previously allocated block of memory for an object created with the new operator. These definitions contain the main difference between the new operator and the delete operator in C++.

Use What’s diff btw new operator and delete operator in C++?

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

conclusion

The basic difference between new operator 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 memory allocated using the new operator.

Leave a Reply

Your email address will not be published.

Back to top button