Simple delete Memory Releasing Operator Example Program In C++
Definition
- In C++, delete operator, is used to release memory or de-allocates memory that was previously allocated by the new operator at runtime or end of the program.
- The delete operator intimates the newly(using new operator) allocated memory address into the system, which memory is released by the system.
- Every new must be matched by a call to delete; failure to do so causes memory leaks.
Syntax
data_type *ptr;
ptr = new data_type[size];
//releasing memory for ptr variable using delete Operator
delete [] ptr;
for example,
int *ptr;
ptr = new data_type[10];
//releasing memory for ptr variable using delete Operator
delete [] ptr;
delete Memory Releasing Operator Example Program
// Header Files
#include<iostream>
#include<conio.h>
#include<stdio.h>
//Main Function
using namespace std;
int a = 100; //Global variable
int main() {
// Local Variable Declaration
int num, *ptr, i;
cout << "Simple delete Memory Releasing Operator Example Program In C++\n";
cout << "Enter the Number for Dynamic Array : ";
cin >> num;
// Dynamic Array Created
ptr = new int[num];
cout << "Dynamic Array Created :" << num;
cout << "\nEnter Values for Dynamic Array :\n";
for (i = 0; i < num; i++)
cin >> ptr[i];
cout << "\nDynamic Array Values are\n";
for (i = 0; i < num; i++)
cout << ptr[i] << endl;
cout << "\nReleasing memory Using delete Operator\n";
// Releasing memory for ptr variable using delete Operator.
delete [] ptr;
cout << "\nNow ptr Printing Garbage Values\n";
for (i = 0; i < num; i++)
cout << ptr[i] << endl;
//Main Function return Statement
return 0;
}
Sample Output
Simple delete Memory Releasing Operator Example Program In C++
Enter the Number for Dynamic Array : 4
Dynamic Array Created :4
Enter Values for Dynamic Array :
100
200
300
400
Dynamic Array Values are
100
200
300
400
Releasing memory Using delete Operator
//Garbage Values are different depend upon system memory
Now ptr Printing Garbage Values
1514000
0
1507664
0
C++ Operator Example Programs
- Simple Arithmetic Operators Example Program In C++
- Simple Relational Operator Example Program In C++
- Simple Logical Operators Example Program In C++
- Simple Assignment Operators Example Program In C++
- Simple Unary Operators Example Program In C++
- Simple Conditional or Ternary Operators Example Program In C++
- Simple Comma Operators Example Program In C++
- Simple Scope Resolution Operator Example Program In C++
- Simple new Memory Allocation Operator Example Program In C++
- Simple delete Memory Releasing Operator Example Program In C++
Read More Articles
- Simple Merge Sort Program in C++
- Scope Resolution Operator In C++
- Simple Program for Virtual Functions Using C++ Programming
- Simple Class Example Program For Find Prime Number In C++
- Simple Example Program For Parameterized Constructor In C++
- Define Constructor in Outside Class Example Program In C++
- Simple Program for Function Overloading Using C++ Programming
- Simple Example Program For Copy Constructor In C++
- Simple Program for Single Inheritance Using C++ Programming
- Simple Program for Inline Function without Class Using C++ Programming
- Factorial Using Function Example Program In C++
- Simple Addition ( Add Two Integers ) Example Program
- Simple Example Program For Constructor Overloading In C++
- Simple Example Program for Inline Function Using C++ Programming
- Simple Example Program For Constructor In C++
- Simple Program for Read user Input Using cin
- Factorial Using Loop Example Program In C++
- Simple Stack Program in C++ Programming
- Simple Program for Friend Function Using C++ Programming
- Simple Program for Static Data and Member Function Using C++ Programming
- Simple Program for Unary Operator Overloading Using C++ Programming
- Do While Loop Example Program In C++
- Simple Program for Multiple Inheritance Using C++ Programming
- Simple Copy Constructor Example Program For Find Factorial In C++
- Simple Program for Exception Handling Divide by zero Using C++ Programming