1

I would like to define an array of array eg int[][] on top of an existing int[] of the right size. I want then to seamlessy use either the int[][] array of array or the int[] 'big' array while refering to the same inner data.

Is this possible to do this in C# using unmanaged code? In C++ I would defined pointers like this :

int* bigArray = new int[numberOfRows * numberOfColumns];
int** matrix = new int*[numberOfRows];

for (int i = 0; i < numberOfRows; i ++)
{
    matrix[i] = (bigArray + i * numberOfColumns);
}
1
  • can be easily achieved. and you dont need unsafe code for this... if at all possible you should avoid unsafe code. Commented Dec 20, 2009 at 20:53

2 Answers 2

2

Yes, you can also use pointer in C#, but there are some limits on it. 1st, you need to declare the function with unsafe, and also, you need to set the build option to allow unsafe code in the project property, here is the sample code about it:

        unsafe static void T1(int numberOfRows, int numberOfColumns)
    {
        int* bigArray = stackalloc int[numberOfRows * numberOfColumns];
        int** matrix = stackalloc int*[numberOfRows];

        for (int i = 0; i < numberOfRows; i++)
        {
            matrix[i] = (bigArray + i * numberOfColumns);
        }

    }

Remember, you can use the pointer only if you are so clear of that you have full control of your code. while it is not recommend you use the pointer in C#

Sign up to request clarification or add additional context in comments.

4 Comments

Is there a way to cast int** matrix to an actual int[][] and int* bigArray to int[]? Cause that's what I would like to do : declare both objects whith this given property then use them seamlessy in classical managed C# code which expects int[] and int[][]. Last precision, do I have to free/delete pointers? I guess I do have to, don't I?
You need to pin this array during it's entire lifetime.
Sorry, I can't get what exactly you wanna to do, in fact, the managed code for C# now can do mostly of what the pointer can do, so I think you'd better try to implement what you want to do without these unsafe code. BTW, you needn't to free the pointers because the GC will do this work if the pointer was out of scope. Sorry Not hlep you so much.
Well, I probably make myself not clear, but the managed solution doesn't do it because it doesn't actually prodide an int[][] object pointing to the same data as in the int[] object. Your solution is closer so far! But thanks a lot both of u for your answers
2

You can do it in managed code, using a class as a wrapper and providing it with an indexer:

class Matrix
{
    public readonly int[] BigArray;   // make available
    private int _rowSize = ...;

    public int this[int x, int y]
    {
       get { return BigArray [x*_rowSize+y]; }
       set { BigArray [x*_rowSize+y] = value; }
    }
}

Edit:

changed arrray into public readonly field so that the class has a dual interface

Edit 2:

Using T[][] to provide access to T[] rows.

class Matrix2<T>
{
    private T[][] _data;
    private int _columns;

    public Matrix2(int rows, int cols)
    {
        _data = new T[rows][];
        _columns = cols;
        for (int i = 0; i < rows; i++) _data[i] = new T[cols];
    }

    public T this [int x]
    {
        get { return _data [x/_columns][x % _columns]; }
        set { _data [x/_columns][x % _columns] = value; }
    }

    public T[] Row(int r)
    {
        return _data [r];
    }
}

7 Comments

The problem is that I want the resulting 'matrix' to physically be an array, not just implementing the indexer. ie i want to pass the resulting matrix to functions expecting for an array. But thanks for your answer!
That is no problem, I changed it a little.
What do you mean by dual interface? I've slightly edit my question to make myself clearer. In your example I cannot simultaneously use int[] BigArray and the array of array int[][] that we can call MatrixArray
Guillaume, You can use both m[i,j] and m.BigArray[i*rows+j], as close as you get without unmanaged.
Well not quite, I know that you defined indexers [] and [,] but the Matrix class is not an array of arrays as such, for example I cant give i to a function waiting for an int[][] parameter, which is crucial for what i want to do.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.