pin_ptr and jagged arrays.

I can't seem to find any specific information on pinning jagged arrays. For example:


void SomeMethod(array<array<Byte>^ >^ data)
{
// ...
pin_ptr<Byte> pinnedPointer = &data[0][0];
//...
}

Does this have the effect of pinning the entire data object or only the array data[0] The docs for pin_ptr only specify that it pins the entire array; but array<array<Byte>^ >^ is an array of arrays.




Answer this question

pin_ptr and jagged arrays.

  • Music

    I read in MSDN, it isn't really clear but this is what I understand it does.

    The sub-object is the item in the array in the array, so the object that gets pinned is the array in the array. The array containing arrays is not pinned.

    It would seem most logical.



  • Teo97917

    Yes, that's kind of what I was assuming too.

    The problem with that is that, since pin_ptr must be allocated on the stack and cannot be an element of an unmanaged array, you can't use pin_ptr to pin a dynamic number of managed objects (arrays in this case).

    For example, if I have a jagged array (an array of arrays) and all array elements need to be used by unmanaged code, I have no way of using pin_ptr to pin that arbitrary number of elements.  I would have to know at compile-time how many elements are in the array and declare that number of pin_ptr variables to deal with it.



  • Maggie Yin

    Yes, that is what I've been forced to use. pin_ptr seemed like such a cleaner alternative.

  • MikeCai

    You can get the same effect using GCHandle.Alloc with GCHandleType.Pinned and GCHandle.AddrOfPinnedObject method. This is not restricted to stack allocated objects.
  • pin_ptr and jagged arrays.