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
Kyle0654 - 24 May 2004 15:17 GMT
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[i] = (int*)malloc(sizeof(int) *
iLengthD2);
}[/code:1:7281b13e40]
Then reference a cell like normal:
[code:1:7281b13e40]iDArr[2][3] =
6;[/code:1:7281b13e40]
B0nj - 24 May 2004 19:29 GMT
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.
> Hi,
>
[quoted text clipped - 11 lines]
>
> George Zou
Brandon Bray [MSFT] - 25 May 2004 02:37 GMT
> 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!

Signature
Brandon Bray, Visual C++ Compiler http://blogs.msdn.com/branbray/
This posting is provided AS IS with no warranties, and confers no rights.
PaulW - 25 May 2004 15:21 GMT
You are trying to create a Jagged array in Managed C++
Unfortunately they are not supported, please see http://support.microsoft.com/?id=814058
They are supported in the next release of the managed C++ tools however
I created a small C# assembly to handle just this sort of thing.