2

I have a 2d array a[3,3]. How can I express one dimension as a new array and pass it to some function?

int[,] a = new int[3,3];

a[0,0] = 1;

...

string b = concatenate(a[0]);     // where concatenate is a function
                                  // take a one dimension array as param

Also, can I create a 65000x65000 array with C#? I got some "out of memory" error.

dtb
  • 213,145
  • 36
  • 401
  • 431
Mavershang
  • 1,266
  • 2
  • 15
  • 27
  • 8
    dear user208080! this site is **community**, not just place to get help. please respect the rules. you asked 5 questions and didn't mark any of answers as accepted. be grateful! and format your code! – Andrey Jun 07 '10 at 17:53
  • 6
    65000x65000 int array = 15.7393515 gigabytes in memory at once. Yes you *can* do it if you have enough free memory, but now the question here is if you *should* do it ;) – Juliet Jun 07 '10 at 17:56
  • What problem are you trying to solve? – Hamish Grubijan Jun 07 '10 at 17:57
  • 1
    @Juliet in 32bit system you can't, even if you have that much physical memory – Andrey Jun 07 '10 at 18:48

4 Answers4

7

The easiest way to handle this is to create a jagged array

int[][] i = new int[3][];

that way:

string b = concatenate(i[0]); will work.

To your second question you will run into issues with the LOH with objects approaching that size. This is probably not your problem though. I would look here as to a possible explanation as to why.

Community
  • 1
  • 1
kemiller2002
  • 113,795
  • 27
  • 197
  • 251
3

You will need to use jagged arrays.

int[][] a = new int[3][]
a[0] = new int[3];
a[1] = new int[3];
a[2] = new int[3];
a[0][0] = 1;
string b = concatentate(a[0]);

Also, creating a 65000x65000 array would result in 65000^2 = 4225000000 slots (or about 16GB or data) so it is no wonder that you are getting an OutOfMemoryException.

Brian Gideon
  • 47,849
  • 13
  • 107
  • 150
2

To answer your second question, a 65k x 65k two-dimensional array has more than 4 billion items; a 4-byte integer type would clock in at about 16GB in size. If you don't have enough memory on your box to initialize a 16GB block of memory, then, no, you cannot create a 65k x 65k array. And that's not even bothering with the addressing issues you'll run into trying to do a lookup on that array. Too big, man. Too big.

To answer your first question, a two-dimensional array is a unique construct that is incompatible with one-dimensional arrays. If you create a jagged array instead (which is an array of arrays), you'll be able to do what you want.

Randolpho
  • 55,384
  • 17
  • 145
  • 179
  • @Andrey: heh... excellent point. I should have said something like "4 billion items". I'll edit appropriately. – Randolpho Jun 07 '10 at 20:50
1

A 65000 by 65000 array of ints would take about 16 gigabytes of memory, so it's not surprising you got an out of memory error. I couldn't get a single .NET 3.5 application to allocate more than about 1.7GB of memory when I last tried, so no luck there.

As for passing one dimension to a function, it depends. You may be able to do what you want with a jagged array:

int[][] array = new int[3][];
for (int i = 0; i < array.Length; i++)
    array[i] = new int[3];

string b = concatenate(array[0]);

That way each position in the first dimension is its own array, which you can pass around (as in the last line). If you wanted to pass a row of items in the other 'direction', you'd have to copy items around first.

PS: You might want to look at String.Join and String.Concat for your concatenating needs. The second has a lot of overloads, but you probably need the first for displaying integers.

Thorarin
  • 47,289
  • 11
  • 75
  • 111
  • @user208080: You're welcome. It's customary to up-vote useful answers and mark whatever best answers your question as the accepted answer. Not saying that's mine, but if you accept useful answers, you're more likely to get help in the future. Also, it makes SO a better website as a reference for other people. They won't have to read all the answers, because the best one is on top of the list :) – Thorarin Jun 07 '10 at 18:42