Using multidimensional array in managed C++

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

I have a line of C# code like following, and trying to rewrite it in managed C++, but failed after many try.

C# code

int[][] params = new int[length][]

C++ code I tried, but could not get it to compile

int params __gc[,] = new int __gc[length,]

Anyone knows how to the C++ code to work? Thanks in advance

George Zo
 
First make a double-pointer:

int** iDArr;

Then allocate enough memory for the first dimension of pointers:

[code:1:7281b13e40]iLengthD1 = 5; // The first dimension's size
iDArr = (int**)malloc(sizeof(int*) *
iLengthD1);[/code:1:7281b13e40]

Then allocate memory for the second dimensions:

[code:1:7281b13e40]iLengthD2 = 4; // The second dimension's size
for (int i = 0; i < iLengthD1; i++)
{
iDArr = (int*)malloc(sizeof(int) *
iLengthD2);
}[/code:1:7281b13e40]

Then reference a cell like normal:

[code:1:7281b13e40]iDArr[2][3] =
6;[/code:1:7281b13e40]
 
Just don't go there. I've been down that road and it's a complete dead end.
Keep it in C#, and use unmanaged DLLs if you need raw c++ code.

George Zou said:
Hi,

I have a line of C# code like following, and trying to rewrite it in
managed C++, but failed after many try.
C# code:

int[][] params = new int[length][];

C++ code I tried, but could not get it to compile:

int params __gc[,] = new int __gc[length,];

Anyone knows how to the C++ code to work? Thanks in advance.

George Zou
 
George said:
I have a line of C# code like following, and trying to rewrite it in
managed C++, but failed after many try.

C# code:

int[][] params = new int[length][];

In the syntax supported by VC7.0 and VC7.1, there is no direct translation.
In the new C++ syntax that is included with VC8.0 (Codenamed Whidbey, and
going into Beta this summer), the syntax is as follows:

array<array<int>^>^ params = gcnew array<array<int>^>(length);

Hope that helps!
 
Back
Top