question about arrays

C

Chris

Hi,

are managed arrays of value types created on the managed heap, just as are
managed arrays of reference types ?

String* StrArray[] = new String* [5]; // Arrays of a Reference type
int NumArray __gc[] = new int __gc[5]; // // Arrays of a Value type

So both 'StrArray' and 'NumArray' are located on the heap ? and the only
difference is that 'StrArray' contains pointers to String, and 'NumArray'
contains its values directly.
Is that correct ?

thnx
Christian
 
B

Ben Schwehn

are managed arrays of value types created on the managed heap, just as are
managed arrays of reference types ?

yep. Everything is created on the managed heap.

String* StrArray[] = new String* [5]; // Arrays of a Reference type
int NumArray __gc[] = new int __gc[5]; // // Arrays of a Value type

So both 'StrArray' and 'NumArray' are located on the heap ? and the only
difference is that 'StrArray' contains pointers to String, and 'NumArray'
contains its values directly.
Is that correct ?

Not entirely since you don't have real pointers as you're used to in
native c++; rather StrArray contains handles to the gc-controlled
memory. You can think of an handle as a pointer to a lookuptable that
contains the real memory address. This is important since the memory
location of your objects doesn't neccessary stay constant (the gc moves
objects around as it compacts the heap). If you want to pass managed
objects to unmanaged code, you have to make sure the gc doesn't move
things around by pinning the objects (using __pin).

In the next version of managed c++ (download the beta at
lab.msdn.microsoft.com/vs2005) this difference between pointers and
handles is also reprsented in the syntax. Instead of String* you would
then write String^.

I hope what I'm saying here is correct :)
 
R

Ronald Laeremans [MSFT]

Hi Ben,
Not entirely since you don't have real pointers as you're used to in
native c++; rather StrArray contains handles to the gc-controlled memory.
You can think of an handle as a pointer to a lookuptable that contains the
real memory address. This is important since the memory location of your
objects doesn't neccessary stay constant (the gc moves objects around as
it compacts the heap). If you want to pass managed objects to unmanaged
code, you have to make sure the gc doesn't move things around by pinning
the objects (using __pin).

You got one aspect partially incorrect. Hanldes do not use an indirection.
They point directly to the actual memory location in the GC heap. When the
GC moves an object it updates all the handles that are pointing to it
directly. Your comment about pinning is still completely acurate since that
tells the GC not to move the momory because you have code that is holding a
direct poinjter that the GC does not know about and thus could not update.

Ronald Laeremans
Visual C++ team
 
B

Ben Schwehn

You got one aspect partially incorrect. Hanldes do not use an indirection.
They point directly to the actual memory location in the GC heap. When the
GC moves an object it updates all the handles that are pointing to it
directly.

Thanks for correcting this. I read an article or posting recently
claiming it used indirection and didn't bother thinking much about it
let alone reading the docs...

ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top