In managed/unmanaged array interoperability, I have a case of not having the usual
fixed (byte* data = new byte[length])
{
// work with the array
}
but rather, I want to pin an array where I only get a reference, like this:
IntPtr dataPtr = camera.Image2d.GetDataBuffer();
fixed (byte* data = (byte *)dataPtr)
{
// work with the array
}
1) From my understanding, the bottom code should also work, since ‘fixed’ will pin a memory location low level in the memory manager, and not care about any objects pointing to it? (Meaning, I do not have the “root”/a direct pointer – even though there probably is not any such concept.)
One additional question:
2) The requirement to use ‘fixed’ comes from the CLR memory manager running concurrently to any executed code, thus it could move arrays at any time?
1) the second one does not seem proper as using the
fixedkeyword you are trying to pin the pointer, not the actual object. AndIntPtris not even (afaik) a managed pointer, rather an unmanaged one.2)
fixedcreates a pointer to the specified managed variable; and without pinning, GC may relocate the variable to another memory location, thus pointer will become useless.from msdn: