3 ms·
How would you express a 2D memory layout with only pointers?
by uecker 4y ago
How would you express a 2D memory layout with only pointers?
- colejohnson66 4y agoAn array of pointers to arrays? Basically, a `T**` C#'s "jagged" arrays are like this, and to get a "true" 2D array, you use different syntax (a comma in the indexer): int[][] jagged; // an array of `int[]` (i.e. each element is a pointer to a `int[]`) int[,] multidimensional; // a "true" 2D array laid out in memory sequentially // allocate the jagged array; each `int[]` will be null until allocated separately jagged = new int[][10]; Debug.Assert(jagged.All(elem => elem == null)); for (int i = 0; i < 10; i++) jagged[i] = new double[10]; // allocate the internal arrays Debug.Assert(jagged[i][j] == 0); // allocate the multidimensional array; each `int` will be `default` which is 0 // element [i,j] will be at offset `10*i + j` multiDimensional = new double[10, 10]; Debug.Assert(multiDimensional[i, j] == 0);
- uecker 4y agoYes, this is people with pre-C99 compilers that do not support variably modified types sometimes do. It is horrible (although there are some use cases).