📜  如果一个对象是在 C++ 中的块内创建的,那么它存储在哪里?

📅  最后修改于: 2021-10-28 02:07:07             🧑  作者: Mango

有两个内存部分可以存储对象:

  1. 堆栈堆栈中的内存由在块/函数内声明的所有成员使用。请注意, main 也是一个函数。
  2. – 此内存未使用,可用于在运行时动态分配内存。

在块或函数内创建的对象的范围仅限于创建它的块。

  • 在块内创建的对象将存储在堆栈中,当函数/block 退出时,对象将被销毁并从堆栈中移除。
  • 但是如果我们在运行时创建对象,即通过动态内存分配,那么对象将被存储在堆上。这是在new运算符的帮助下完成的。在这种情况下,我们需要使用delete 运算符显式销毁对象。

例子:

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

下面是显示对象存储位置的程序:

// C++ program for required implementation
#include 
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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程