Alex Rivera | Logout

Are these objects's references on the Stack or on the Heap?

Asked 2010-04-01T09:09:59.730
15

I would really appreciate if someone could tell me whether I understand it well:

class X
{
   A a1=new A(); // reference on the stack, object value on the heap
   a1.VarA=5;    // on the stack - value type
   A a2=a1;      // reference on the stack, object value on the heap
   a2.VarA=10;   // on the stack - value type         
}

Also both a1 and a2 references are on the stack, while their "object" values are on the heap. But what about VarA variable, its still pure value type?

class A
{
   int VarA;
}
Edit
Report

1 Answer

2

In this case a1.VarA would be on the heap as space for it would have been allocated when you did A a1 = new A().

If you just do int i = 5; in a function that will go on the stack but as you explicitly stated a1 was to be allocated on the heap then all value types associated with it will be placed on the heap

answered 2010-04-01T09:16:23.673

Your Answer