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.
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- How to put the text inside of a created icon?
- How to Cast a JSON Object Inside of TypeScript Class ?
- Why was C++ created?
- Why does SASS cache folder is created ?
- Advantages and Disadvantages of Using Stored Procedures - SQL
- Sort numbers stored on different machines
- What is the difference between created and mounted event in VueJS?
- How to call function from it name stored in a string using JavaScript?
- How to handle events in dynamically created elements in jQuery ?
- How to block comments in YAML ?
- HTML | Block and Inline Elements
- Catch block and type conversion in C++
- How to select first object in object in AngularJS?
- How to check a JavaScript Object is a DOM Object ?
- How to add `style=display:“block”` to an element using jQuery?
- How to remove the space between inline-block elements?
- How to prevent inline-block divs from wrapping ?
- Index inside map() Function
- How to place a div inside an iframe for IE ?
- Which Characters Should Be Escaped Inside A "pre" tag?
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.

