2
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 ?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Amruth A
  • 66
  • 5
  • 17
  • 2
    'Where is local variable of ... function created': on the stack. 'Member': irrelevant. 'If object is created via `new`': irrelevant. – user207421 Aug 01 '19 at 12:06
  • 1
    The "local variable" is created whenever the function is called, and ceases to exist when the function returns. That has no relationship whatsoever to how the object is created. Note: since the variable is not used, the compiler may optimise it out of existence, since there would be no observable difference in the program that can be detected using standard C++ (examining the code emitted by the compiler may reveal if the variable ever exists, but that is outside the realm of standard C++) – Peter Aug 01 '19 at 12:39

4 Answers4

6

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
3

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

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
2

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.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

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

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335