views:

418

answers:

6

Say I've got a class like this:

class Test
{
  int x;
  SomeClass s;
}

And I instantiate it like this:

Test* t = new Test;

Is x on the stack, or the heap? What about s?

+6  A: 

Since you've used new, it's all on the heap, stored [more or less] contiguously in t's memory area.

Avdi
+16  A: 
Test a;
Test *t = new Test;

a, and all its members, are on the stack.

The object pointed to by t, and all its members, are on the heap.

The pointer t is on the stack.

moonshadow
that should be "*t, and all its members, is on the heap". t itself is on the stack.
James Curran
+5  A: 

t is on the stack. The object at *t is on the heap. It contains an int and a SomeClass object next to each other in a unit.

James Curran
+1  A: 

Since you're using new, you're allocating your object on the heap. Consequently, every members of the Test pointed by t are on the heap too.

Luc Touraille
+4  A: 

Each time you "instantiate" an object/symbol using a new (we are speaking C++ here), a new memory zone will be allocated for this object. If not, it will be put on the "local" memory zone.

The problem is that I have no standard definition for "local" memory zone.

An example

This means that, for example:

struct A
{
   A()
   {
      c = new C() ;
   }

   B b ;
   C * c ;
}

void doSomething()
{
   A aa00 ;
   A * aa01 = new A() ;
}

The object aa00 is allocated on the stack.

As aa00::b is allocated on a "local" memory according to aa00, aa00::b is allocated inside the memory range allocated by the new aa01 instruction. Thus, aa00::b is also allocated on stack.

But aa00::c is a pointer, allocated with new, so the object designed by aa00::c is on the heap.

Now, the tricky example: aa01 is allocated via a new, and as such, on the heap.

In that case, as aa01::b is allocated on a "local" memory according to aa01, aa00::b is allocated inside the memory range allocated by the new aa01 instruction. Thus, aa00::b is on the heap, "inside" the memory already allocated for aa01.

As aa01::c is a pointer, allocated with new, the object designed by aa01::c is on the heap, in another memory range than the one allocated for aa01.

Conclusion

So, the point of the game is:
1 - What's the "local" memory of the studied object: Stack of Heap?
2 - if the object is allocated through new, then it is outside this local memory, i.e., it is elsewhere on the heap
3 - if the object is allocated "without new", then it is inside the local memory.
4 - If the "local" memory is on the stack, then the object allocated without new is on the stack, too.
5 - If the "local" memory is on the heap, then the object allocated without new is on the heap, too, but still inside the local memory.

Sorry, I have no better vocabulary to express those concepts.

paercebal
Actually, that's a very good answer. I like your usage of the word "local", it makes all the concepts a lot clearer. There's a couple of places where your language could be improved, but it's a very good explanation nonetheless. Thank you!
fluffels
I'll try to find a better way of expressing all this, with a lot of less text, but clearer... :-) ...
paercebal
+1  A: 
class MyClass {
    int i;
    MyInnerClass m;
    MyInnerClass *p = new MyInnerClass();
}

MyClass a;
MyClass *b = new MyClass();

a is on the stack; its members a.i and a.m (including any members of a.m) and a.p (the pointer, not the object it points to) are part of it and so also on the stack.

The object pointed to by a.p is on the heap.

The object pointed to by b is on the heap, including all its members; and so is the object pointed to by b.p.

DJClayworth