- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Where is an object stored if it is created inside a block in C++?
In this section we will discuss about where the variables and objects are stored in the memory when a C++ program is compiled. As we know, there are two parts of memory in which an object can be stored −
Stack − All members that are declared inside block of memory, it stores inside the stack section. The main function is also a function, so elements inside it will be stored inside the stack.
Heap − When some objects are allocated dynamically, then that is stored inside the heap section.
The scope of the object declared inside a block or a function is limited to the block in which it is created. The object will be stored in the stack when it is created inside the block and when the control exits the block or function then the object is removed or destroyed.
In case of dynamically allocated objects (during runtime) the object will be stored on the heap. This is done with the help of new operator. To destroy that object, we have to use del keyword to destroy it explicitly.
Example
Let us see the following implementation to get better understanding −
#include <iostream> using namespace std; class Box { int width; int length; public: Box(int length = 0, int width = 0) { this->length = length; this->width = width; } ~Box() { cout << "Box is destroying" << endl; } int get_len() { return length; } int get_width() { return width; } }; int main() { { Box b(2, 3); // b will be stored in the stack cout << "Box dimension is:" << endl; cout << "Length : " << b.get_len() << endl; cout << "Width :" << b.get_width() << endl; } cout << "\tExitting block, destructor" << endl; cout << "\tAutomatically call for the object stored in stack." << endl; Box* box_ptr;{ //Object will be placed in the heap section, and local pointer variable will be stored inside stack Box* box_ptr1 = new Box(5, 6); box_ptr = box_ptr1; cout << "---------------------------------------------------" << endl; cout << "Box 2 dimension is:" << endl; cout << "length : " << box_ptr1->get_len() << endl; cout << "width :" << box_ptr1->get_width() << endl; delete box_ptr1; } cout << "length of box2 : " << box_ptr->get_len() << endl; cout << "width of box2 :" << box_ptr->get_width() << endl; }
Output
Box dimension is: Length : 2 Width :3 Box is destroying Exitting block, destructor Automatically call for the object stored in stack. --------------------------------------------------- Box 2 dimension is: length : 5 width :6 Box is destroying length of box2 : 0 width of box2 :0