0

I understand that objects initialized with 'new' are allocated from the heap, but what about their members? For example, I have class A:

class A
{
        private: int a;   //here "a" should be on stack  
};

Then I have object A defined in following code respectively

A a;

A *ap = new A();

Now the first statement places a on stack and ap will be in the heap, but how about a.a and ap->a? Are they with their parent objects?

Derek
  • 1,104
  • 13
  • 35
Korben
  • 734
  • 1
  • 7
  • 26

2 Answers2

1

Yes, by definition if an object is on the heap, so are its members, and likewise for the stack.

An object is really just a chunk of memory, so because the object contains the member, thats where the memory for the member will be.

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
0

The members are part of the object, just like with a struct.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278