views:

92

answers:

2

I have a multidimentional array of pointers to integer (of unknown rank) being passed into my function as such:

    public unsafe static void MyMethod(Array source, ...)
    {
         //...
    }

Multidimensional arrays of pointers are being constructed outside of the method and being passed in. Here's an example:

int*[,,,] testArray = new int*[10,10,5,5];

MyMethod(testArray);

How can I set a value at an runtime-computed index in the array? Array.SetValue(...) works perfectly fine for non-pointer arrays, but refuses to work for my int* array. Using reflector, I see SetValue reduces down to calling InternalSetValue which takes an object for the value but it's marked as extern and I can't see the implementation. I took a shot in the dark and tried passing in boxed pointer, but no luck.

+1  A: 

This doesn't work because it's not possible to box a pointer in .NET, so you can never call the Array.SetValue and pass an int*.

Can you declare MyMethod to accept int*[,,,] instead?

Edit: for further reading, an interesting recent post from Eric Lippert.

Tim Robinson
The requirement for the method is that it works with an array of any dimension. Also, see: http://msdn.microsoft.com/en-us/library/system.reflection.pointer.box.aspx
Robert Venables
OK, but Array.SetValue doesn't know how to unbox a pointer boxed using Pointer.Box: I get "System.NotSupportedException: Type is not supported."
Tim Robinson
I found that out (see above) - It's actually Array.InternalGetReference (also marked as extern) throwing that exception.
Robert Venables
+4  A: 

This works:

unsafe static void MyMethod(int** array)
{
    array[10] = (int*)0xdeadbeef;
}

private static unsafe void Main()
{
    int*[, , ,] array = new int*[10, 10, 10, 10];

    fixed (int** ptr = array)
    {
        MyMethod(ptr);
    }

    int* x = array[0, 0, 1, 0]; // == 0xdeadbeef
}

Does that help?


Question to the experts: Is it wrong to assume that the array is allocated consecutively in memory?

dtb
No, it's not wrong. Arrays are allocated consecutively.
Maximilian Mayerl
@Maximilian: Thanks. Is this documented somewhere? Or is it an implementation detail if the CLR?
dtb
Yes, this is specified by Ecma-335 (CLI spec): "Array elements shall be laid out within the array object in row-major order (i.e., the elements associated withthe rightmost array dimension __shall be laid out contiguously__ from lowest to highest index)."
Pavel Minaev
@Pavel: Thanks. Great.
dtb
Close Enough. Thanks dtb!
Robert Venables