SoFunction
Updated on 2025-03-07

C#'s jagged array and C++ implementation code

in principle:

1、The serrated array is first a two-dimensional array, and the dimension of the first dimension is determined.

2、The reason why flexible jagged arrays can appear in C# is because the arrays in C# are reference types (essentially, they store pointers)

According to the concept of this reference type (pointer), pointer arrays can also be implemented in C++

In C#:

class A{}

int szA[][] = new int[3][];

szA[0] = new int[2];

szA[1] = new int[6];

szA[2] = new int[3];

Of course, the actual array elements (shaping variables) have not yet initialized the value, so C# does not allow uninitialized variables to be used

In C++, you can define an array of int* pointer variables, with members of three int* pointer variables, and then each pointer variable allocates memory of 2, 6, and 3 int variables on the heap memory.

This is OK!

In fact, it uses the concept of pointers. However, the syntax of C# seems simpler in form, but it also loses the flexibility to directly operate the underlying memory values. Hosted and non-hosted, .net platforms have both advantages and disadvantages.