1

I have a question about Jagged arrays in C#, as i read about Jagged arrays somewhere on the internet, i think that the memory allocation of 2d Jagged arrays in C# is the same as memory allocation of 2d arrays in C++, because a 2d Jagged array has an array of pointer that each pointer refers to an array of elements (for example integer elements) i mean that memory allocation of the array bellow in C++ :

int** twoDArr {new int* [number1]};
for (int i = 0; i < number1; i++)
{
   twoDArr[i] = new int[number2];
}

is the same as memory allocation of 2d Jagged arrays in C# :

int[][] 2DJaggedArray = new int[number1][];
for (int i = 0; i < 2DJaggedArray.GetLength(0); i++)
{
    2DJagggedArray[i] = new int[number2];
}

But i am not sure about , So could you please tell me if i am right and if so, could you please explain me how is memory allocation of 2d array in C# for example array bellow:

int[,] 2DArray = new int[number1,number2];

Thanks.

A.R.S.D.
  • 180
  • 3
  • 13
  • 1
    I don't think identifier in C++ can start with a digit... Can the code compile? – user202729 Feb 04 '18 at 07:38
  • Dupe: https://stackoverflow.com/q/11575735/5267751 – user202729 Feb 04 '18 at 07:38
  • I edited that. Thanks , But my question is not the same as the question in the link above , i am sure that in the link above there is no comparence in memory allocation of the arrays in C++ and C#. – A.R.S.D. Feb 04 '18 at 08:07
  • Possible duplicate of [Two-dimensional array in memory](https://stackoverflow.com/questions/11575735/two-dimensional-array-in-memory) – Martin Zikmund Feb 04 '18 at 08:11
  • nope , because in the link you have mentioned there is not any explanation on pointers and how 2d arrays in C# use pointers in memory allocation process. – A.R.S.D. Feb 04 '18 at 08:16

2 Answers2

2

Yes you are right. A Jagged array in C# is basically an single dimension array in memory where each element is just an reference. When you initialize the array in the for loop, it creates a new array somewhere else in memory and the reference points to it.

In case of multidimensional arrays ([,]), the situation is very different. When you initialize such array, a single block of memory is created and it's size is equal to the product of all dimensions of the array. Technically having a multidimensional array of size [M,N] is represented in memory the same way as a simple array of size [M * N]. The access by multiple indices is basically just a syntactic sugar where the framework calculates the actual position of the element by multiplication of the dimensions.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • Thanks for your answer , that was what i want to know. But another question remained unexplained and that is ; do the Jagged arrays process of memory allocation is the same as C++ multi-dimensional arrays ? – A.R.S.D. Feb 04 '18 at 09:13
  • It is not the same, but it is quite analogous. The difference here is that the whole process is managed by .NET Framework, so there are are additional checks that happen behind the scenes during the construction of the array for example. But the resulting memory allocation is very similar. – Martin Zikmund Feb 04 '18 at 09:21
  • Sure thing. If it helped, please consider accepting the answer as the solution so that it appears as resolved :-) . You can do this by clicking the grey checkmark. – Martin Zikmund Feb 04 '18 at 09:40
  • I know that "your answers are helpful to me" is really obvious, but there is a user (user202729) that really didn't help answering questions but just gave me -1 point just because he /she didn't pay enough attention to understand my question and i am going to be banned from asking question for no reason. – A.R.S.D. Feb 04 '18 at 09:59
  • It seemed the question was duplicate, but because you explained the question more specifically, it shouldn't be taken as such :-) . – Martin Zikmund Feb 04 '18 at 10:20
0

If you are allocating very large multi-dimensional arrays, c# needs to find contiguous memory vs jagged arrays. We had a scenario where we were allocating an excessive number of these large arrays (1500*1500 = 2.5 million of double) and periodically c# would freeze for 20-50 seconds for garbage collection.

Justin
  • 1,303
  • 15
  • 30