Memory Management Operator in C++ language

There are two types of memory management operators in C++:


1. new operator: The new operator in C++ is used for dynamic storage allocation in computer memory. The operator can be used to create the object of any type.
The general syntax of the new operator in C++ is as follow:
datatype pointer_name = new datatype; Here, new is a keyword and the pointer variable is a variable of type datatype.
For example: int *pointer_variable = new int;

2.delete operator: The delete operator in C++ is used for releasing memory space when the object is no longer needed. Once a new operator is used, it is efficient to use the corresponding delete operator for release of memory. 
The general syntax of delete operator in C++ is as follow:
delete pointer_variable; Here delete is a keyword and the pointer_variable is a pointer that points to the objects that are already created with the help of a new operator.

Write a C++ program which demonstrates the use of new and delete operators.

#include<iostream>
using namespace std;
int main()
{
int *ptr = new int; 
*ptr = 10;
cout<<"The output is : ptr = ";
cout<<*ptr;
delete ptr;
return 0;

}

The program output is tested on www.jdoodle.com

Output:
The output is : ptr = 10


Thanks 
Mukesh Rajput

Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: