Jagged arrays and Multidimensional array in C#


In one of the projects, the .NET program needs to allocate a relatively large 3-dimensional array. It was defined as the multidimensional array as the following.

1
private float[,,] data = null;
private float[,,] data = null;

And allocate the space and use it like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
// .NET will allocate 3 dimensions at a time
// the space allocated are continuous.
this.data = new float[100, 100, 100];
for (int x = 0; x < 100; x++)
{
  for (int y = 0; y <100; y++)
  {
    for (int z = 0; z < 100; z++)
    {
       this.data[x, y, z] = 3.1415926f;
    }
  }
}
// .NET will allocate 3 dimensions at a time
// the space allocated are continuous.
this.data = new float[100, 100, 100];
for (int x = 0; x < 100; x++)
{
  for (int y = 0; y <100; y++)
  {
    for (int z = 0; z < 100; z++)
    {
       this.data[x, y, z] = 3.1415926f;
    }
  }
}

However, this will have issues if frequently the array has to be allocated and deallocated. There will be lots of the memory fragments which will sometime fail for such array to be created. Out of Memory exception will then be thrown even there seems to be having enough memory for such arrays.

The workaround is to define the data as jagged array, which, i.e. can be treated as arrays of arrays. The entire array does not need to be continous in memory address space, in fact, only the last dimension is continuous. This will reduce the chance of not having big enough continuous memory address space for this big 3-dimenional array. The definition of the 3-dimensional jagged array is as follows.

1
private float[][][] data = null;
private float[][][] data = null;

The usage is similar but you have to allocate each dimensions separately and manually.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// allocate the first dimension;
this.data = new float[100][][];
for (int x = 0; x < 100; x++)
{
  // allocate the second dimension;
  this.data[x] = new float[100][];
  for (int y = 0; y <100; y++)
  {
    // the last dimension allocation
    this.data[x][y] = new float[100];
  }
}
 
for (int x = 0; x < 100; x++)
{
  for (int y = 0; y <100; y++)
  {
    for (int z = 0; z < 100; z++)
    {
       this.data[x][y][z] = 3.1415926f;
    }
  }
}
// allocate the first dimension;
this.data = new float[100][][];
for (int x = 0; x < 100; x++)
{
  // allocate the second dimension;
  this.data[x] = new float[100][];
  for (int y = 0; y <100; y++)
  {
    // the last dimension allocation
    this.data[x][y] = new float[100];
  }
}

for (int x = 0; x < 100; x++)
{
  for (int y = 0; y <100; y++)
  {
    for (int z = 0; z < 100; z++)
    {
       this.data[x][y][z] = 3.1415926f;
    }
  }
}

The above is recommended if a large multi-dimensional array is required. Addressing elements in jagged array is also quite efficient and there will not any performance hits by using jagged arrays.

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
382 words
Last Post: A Simple Batch Script to Avoid Auto-run Virus/Trojans by Creating Directories
Next Post: 301 and 302 Redirects

The Permanent URL is: Jagged arrays and Multidimensional array in C#

Leave a Reply