简体   繁体   English

如果通过new创建对象,则创建的成员函数的局部变量在哪里?

[英]Where is local variable of member function created if object is created via new?

Class A
{
    int a;
    int get_a()
    {
       int d;
       return a;
    }
};

A* obj_a_ptr = new A;
int c = A->get_a();

Where is int d memory allocated , in heap or stack ? 在堆或堆栈中分配的内存在哪里?

Member functions are not that different from free functions, they only implicitly get a this pointer as first parameter. 成员函数与自由函数没有什么不同,它们只是隐式地将this指针作为第一个参数。 So your member function is more or less equivalent to (lets forget about the fact that nothing in your A is actually accesible, because it is all private ) 所以你的成员函数或多或少等同于(让我们忘记你的A中没有任何东西实际上是可访问的,因为它都是private

int get_a(A* obj)
{
   int d;
   return obj->a;
}

I hope this already answers your question. 我希望这已经回答了你的问题。 Whether obj was allocated via new or not makes no difference for d being on the stack. 是否通过new分配obj对于堆栈中的d没有任何区别。

It should be noted that the C++ specification doesn't actually mandate any specific storage area for local variables, only rules for their life-time and scope. 应该注意的是,C ++规范实际上并没有要求任何特定的局部变量存储区域,只规定它们的生命周期和范围。 But it's common for compilers to use the stack, as that brings some simpler handling when the scope of a function ends. 但是编译器使用堆栈是很常见的,因为当函数的范围结束时,它会带来一些更简单的处理。 And thus all local variables are on the stack. 因此所有局部变量都在堆栈中。

In your example the storage for the variables c and obj_a_ptr will be on the stack. 在您的示例中,变量c obj_a_ptr的存储将位于堆栈中。

Note that the storage for obj_a_ptr is on the stack as well, that is where the value of the variable is stored. 请注意, obj_a_ptr的存储obj_a_ptr堆栈中,即存储变量值的位置。 For a pointer, the variable itself is on the stack (for local variables) but it might point to any suitable object (on the heap or global or anywhere else). 对于指针,变量本身位于堆栈上(对于局部变量),但它可能指向任何合适的对象(在堆上或全局或其他任何位置)。

The [non-static] data members of an object are considered sub-objects , and are stored as part of that object. 对象的[非静态]数据成员被视为子对象 ,并作为该对象的一部分存储。 So your members are "on the heap" if the encapsulating object is "on the heap". 因此,如果封装对象是“在堆上”,那么您的成员就“在堆上”。 (If they weren't, what would be stored there?) (如果不是,那里存储什么?)

However, this does not affect local variables declared inside a member function. 但是,这不会影响在成员函数内声明的局部变量。 There is no need for them to be stored with the object. 它们不需要与对象一起存储。 In fact, this would be very difficult to do at runtime. 实际上,这在运行时很难做到。

When an object is created then memory is allocated for its non-static data members. 创建对象时,将为其非静态数据成员分配内存。 Member functions are not included in the memory occupied by an object except it can contain a pointer to the table of pointers to virtual functions. 成员函数不包含在对象占用的内存中,除了它可以包含指向虚函数指针表的指针。

So within the function get_a the local variable d has automatic storage duration. 因此在函数get_a ,局部变量d具有自动存储持续时间。 After exiting the function the memory may be used by any other function not necessary by a member function of the object. 在退出函数之后,存储器可以由对象的成员函数不必要的任何其他函数使用。

Consider the following demonstrative program 考虑以下示范程序

#include <iostream>

struct A
{
    explicit A( int x = 0 ) : x( x ) {}
    int x;
    static int a[];
    int get() const { return x; }
    void set( int x ) { A::x = x; } 
};

int A::a[100];

int main()
{
    std::cout << "sizeof( A ) = " << sizeof( A ) << '\n';
}

Its output is 它的输出是

sizeof( A ) = 4

So the size of an object of the class in this demonstrative program is exactly equal to the size of its data member x (in general a class can be padded with additional bytes to get an alignment). 因此,此演示程序中类的对象的大小与其数据成员x的大小完全相同(通常,可以使用附加字节填充类以获得对齐)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM