Where is an object stored if it is created inside a block in C++?

There are two parts of memory in which an object can be stored:

  1. stack – Memory from the stack is used by all the members which are declared inside blocks/functions. Note that the main is also a function.
  2. heap – This memory is unused and can be used to dynamically allocate the memory at runtime.

The scope of the object created inside a block or a function is limited to the block in which it is created.

  • The object created inside the block will be stored in the stack and Object is destroyed and removed from the stack when the function/block exits.
  • But if we create the object at runtime i.e by dynamic memory allocation then the object will be stored on the heap. This is done with the help of new operator. In this case, we need to explicitly destroy the object using delete operator.

Examples:



Output:
Inside Block1...
length of rectangle is : 2
width  of rectangle  is :3
Destructor of rectangle
with the exit of the block, destructor called
automatically for the object stored in stack.
*********************************************
Inside Block2
length of rectangle is : 5
width of rectangle  is :6
Destructor of rectangle
length of rectangle is : 0
width of rectangle is :0

Below is the program to show where the object is stored:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for required implementation
#include <iostream>
using namespace std;
  
class Rectangle {
    int width;
    int length;
  
public:
    Rectangle()
    {
        length = 0;
        width = 0;
    }
  
    Rectangle(int l, int w)
    {
        length = l;
        width = w;
    }
  
    ~Rectangle()
    {
        cout << "Destructor of rectangle" << endl;
    }
  
    int getLength()
    {
        return length;
    }
  
    int getWidth()
    {
        return width;
    }
};
  
int main()
{
    // Object creation inside block
    {
  
        Rectangle r(2, 3); // r is stored on stack
        cout << "Inside Block1..." << endl;
        cout << "length of rectangle is : " 
             << r.getLength() << endl;
        cout << "width  of rectangle  is :" 
             << r.getWidth() << endl;
    }
  
    cout << " with the exit of the block, destructor\n"
         << " called automatically for the object stored in stack." 
         << endl;
  
         /* 
          // uncomment  this code and run once you will get
         // the compilation error because the object is not in scope
         cout<<"length of rectangle is : "<< r.getLength();
         cout<< "width of rectangle is :" << r.getWidth();
        */
           
  
    Rectangle* ptr2;
    {
        // object will be stored in heap  
        // and pointer variable since its local
        // to block will be stored in the stack
  
        Rectangle* ptr3 = new Rectangle(5, 6);
        ptr2 = ptr3;
        cout << "********************************************"
             << endl;
        cout << "Inside Block2" << endl;
        cout << "length of rectangle is : " 
             << ptr3->getLength() << endl;
        cout << "width of rectangle  is :" 
             << ptr3->getWidth() << endl;
  
        // comment below line of code and 
        // uncomment *important line* 
        // and then check the object will remain
        // alive outside the block.
  
        // explicitly destroy object stored on the heap
        delete ptr3; 
    }
  
    cout << "length of rectangle is : " 
         << ptr2->getLength() << endl;
  
    cout << "width of rectangle is :" 
         << ptr2->getWidth() << endl;
      
     // delete ptr2;   /* important line*/
  
    return 0;
}

chevron_right


Output:

Inside Block1...
length of rectangle is : 2
width  of rectangle  is :3
Destructor of rectangle
 with the exit of the block, destructor
 called automatically for the object stored in stack.
********************************************
Inside Block2
length of rectangle is : 5
width of rectangle  is :6
Destructor of rectangle
length of rectangle is : 0
width of rectangle is :0


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.