简体   繁体   中英

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. 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 )

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.

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. 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.

Note that the storage for obj_a_ptr is on the stack as well, that is where the value of the variable is stored. 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. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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