Advertisement
//C++ Example of delete operator
#include<iostream>
using namespace std;
int main()
{
//Creating and initializing a float variable using new operator in a single line
double *d = new double(101.4);
cout<< "The float value at the address pointed by pointer variable : " << *d << "\n";
cout<< "The memory address allocated to pointer variable : " << d << "\n";
//Freeing the memory space allocated to the double variable by new operator.
delete d;
cout<< "The float value at the address pointed by pointer variable : " << *d << "\n";
cout<< "The memory address allocated to pointer variable : " << d;
return 0;
}
The float value at the address pointed by pointer variable : 101.4
The memory address allocated to pointer variable : 0x16a5c20
The float value at the address pointed by pointer variable : 0
The memory address allocated to pointer variable : 0x16a5c20
Advertisement
//C++ Using delete operator to deallocate an object
#include<iostream>
using namespace std;
int main()
{
//Creating a memory space for a char array with 5 elements, using new operator
char *arr = new char[5];
arr[0] = 'a';
arr[1] = 'b';
arr[2] = 'c';
arr[3] = 'd';
arr[4] = 'e';
cout<< "The multiple char values in the char array : \n";
for(int i=0;i<5;i++)
{
cout<< arr[i] << "\n";
}
cout<< "The elements contained in an array pointed by pointer variable : " << arr << "\n";
//Deallocating the space allocated to all char elements contained in an array.
delete arr;
//Checking the elements of array after performing delete operation
cout<< "The elements contained in an array after delete operation : " << arr;
return 0;
}
a
b
c
d
e
The elements contained in an array pointed by pointer variable : abcde
The elements contained in an array after delete operation :
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement