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

26 Nov, 2018
There are two parts of memory in which an object can be stored:
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.heap – This memory is unused and can be used to dynamically allocate the memory at runtime.
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.
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:
// 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;}
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

C++-Class and Object
C++-new and delete
cpp-stack
Picked
C++
Heap
Heap
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
HeapSort
K'th Smallest/Largest Element in Unsorted Array | Set 1
Binary Heap
Introduction to Data Structures
Huffman Coding | Greedy Algo-3