A delete operator is used to deallocate memory space that is dynamically created using the new operator, calloc and malloc() function, etc., at the run time of a program in C++ language. In other words, a delete operator is used to release array and non-array (pointer) objects from the heap, which the new operator dynamically allocates to put variables on heap memory. We can use either the delete operator or delete [ ] operator in our program to delete the deallocated space. A delete operator has a void return type, and hence, it does not return a value.

Syntax of delete operator
We can delete a specific element or variable using the delete operator, as shown:
Similarly, we can delete the block of allocated memory space using the delete [] operator.
Program to deallocate memory location of each variable using the delete operator
Let's consider an example to delete the allocated memory space of each variable from the heap memory using the delete operator.
Program1.cpp
Output
Enter first number: 5
Enter second number: 8
Sum of pointer variables = 13
Program to delete the array object using the delete [] operator
Let's create a program to delete the dynamically created memory space for an array object using the delete [] operator in C++.
Program2.cpp
Output
Enter total number of elements to be entered : 7
Enter the numbers:
Number 1 is 45
Number 2 is 600
Number 3 is 78
Number 4 is 93
Number 5 is 29
Number 6 is 128
Number 7 is 32
Numbers are : 45 600 78 93 29 128 32
Program to delete NULL pointer using delete operator
Let's consider a program to delete NULL pointer using the delete operator in C++ programming language.
Program3.cpp
Output
The NULL pointer is deleted.
Delete a pointer with or without value using the delete operator
Let's consider an example to delete a pointer variable with or without a value using the delete operator in C++.
Program4.cpp
Output
The value of ptr is: 1415071048
The value of ptr2 is: 10
Program to allocate dynamic memory using the malloc function and then delete using the delete operator
Let's consider an example of creating dynamic memory using the malloc function and then using the delete operator to delete allocated memory in the C++ programming language.
Program6.cpp
Output
Dynamic memory is deleted using the delete operator.
Program to delete variables of user defined data types
Let's write a program to demonstrate the deletion of user defined object using the delete operator.
Program7.cpp
Output
Custom deletion of size 1
Custom deletion of size 24
Program to delete a void pointer using the delete operator
Let's create a program to release the memory space of the void pointer using the delete operator in C++.
Program8.cpp
Output
The void pointer is deleted using the delete operator.
You may be interested in:
>> Is a Chromebook worth replacing a Windows laptop?
>> Find out in detail the outstanding features of Google Pixel 4a
>> Top 7 best earbuds you should not miss
Related Posts:
>> Recognizing 12 Basic Body Shapes To Choose Better Clothes
>>Ranking the 10 most used smart technology devices
>> Top 5+ Best E-readers: Compact & Convenient Pen
0 Comments:
Post a Comment