Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2004

Tip: Looking for answers? Try searching our database.

Using multidimensional array in managed C++

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
George Zou - 23 May 2004 01:36 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.