Dynamic Memory Allocation and Deallocation


#include "stdafx.h"
#include <iostream>

using namespace std;

int main ()
{  
  // Dynamically allocating integer

  int *ptr = new int/* Here new operator returns the address of the integer and is stored to the pointer ptr. */

  *ptr=10;
  cout<< "Address hold by pointer: "<< ptr << endl;  // Displays the address hold by the pointer.
  cout<< "Content of the memory address hold by the pointer: "<< *ptr << endl; // Content of the memory address hold by the pointer.

  delete ptr; // Deallocate memory assigned to ptr using delete operator.
  ptr = 0;

  cout<< "Address hold by pointer after deallocation: "<< ptr;  

  return 0;
}

Output:
Address hold by pointer: 006C4968
Content of the memory address hold by the pointer: 10
Address hold by pointer after deallocation:00000000