New and Delete Operator in C++

New and Delete Operator in C++

1. new operator is used to allocate memory dynamically at run time. This is called DMA (Dynamic Memory Allocation).
2. delete operator is used to deallocate memory dynamically.
3. new and delete are also called the memory management operator in C++.


new Operator in C++

1. new operator is used to allocate memory to a variable.
2. new is a keyword in C++ which is used for DMA.


For Example:- dynamic memory allocation in C++ with example program
#include<iostream>
using namespace std;

int main() {
 //declaring an int pointer
 int *Ptr;
 
 //DMA using new keyword
 Ptr = new int;
 
 //assigning value to memory
 *Ptr = 99;
}


1. We have dynamically allocated memory for an int variable Ptr using new operator.

2. We have used a pointer for Dynamic Memory Allocation (DMA) because new returns address of the memory location.

3. new returns the first element of array when new is used for allocating memory like array (e.g., int *Ptr = new int[20];).


For Example:- dynamic memory allocation in C++ with example program
#include<iostream>
using namespace std;
class Complex {
 private:
  int real, imaginary;
  
 public:
  void setData(int num1, int num2) {
   real = num1;
   imaginary = num2;
  }
  
  void showData() {
   cout<<real<<" "<<imaginary;
  }
 };
 
int main() {

 //declaring a Complex class pointer
 Complex *Ptr;
 
 //DMA using new keyword
 Ptr = new Complex;
 
 Ptr->setData(9,8);
 Ptr->showData();
}


1. We can also use new and allocate memory for our desired class.

2. We dynamically allocated memory for a Complex class Ptr using new operator.

3. We have used a pointer of Complex class for Dynamic Memory Allocation (DMA) because new returns address of the memory location.


delete Operator in C++

1. We can delete the memory allocated to variable when there is no use of that variable by delete operator keyword.

2. delete returns the memory back to the operating system. This is called memory deallocation.


For Example:- dynamic memory allocation in C++ with example program
#include<iostream>
using namespace std;

int main() {
 //declaring an int pointer
 int *Ptr;
 
 //DMA using new keyword
 Ptr = new int;
 
 //assigning value to memory
 *Ptr = 99;
 
 //printing the value
 cout << *Ptr;
 
 //deallocating allocated memory using delete operator
 delete Ptr;
}


1. delete will not delete the Ptr pointer. It simply deletes the new.
2. DMA helps in making memory management more efficient.


For Example:- dynamic memory allocation in C++ with example program
#include<iostream>
using namespace std;

class Complex {
 private:
  int real, imaginary;
  
 public:
  void setData(int num1, int num2)  {
   real = num1;
   imaginary = num2;
  }
  
  void showData() {
   cout<<real<<" "<<imaginary;
  }
 };
 
 
int main() {
 //declaring a Complex class pointer
 Complex *Ptr;
 
 //DMA using new keyword
 Ptr = new Complex[2];
 
 Ptr->setData(9,8);
 Ptr->showData();
 
 delete []Ptr;
}



<< Previous                                                                                      Next >>
Previous
Next Post »