I have a few questions about multidimensional arrays. I understand how to allocate memory for them, but I don’t see why there are needed (Other than to make things more readable).
The [] operator for the array is overloaded, right? So, why can’t a single block of memory be allocated and access be granted by, 1dArray[i*nInRow][offset]?
Are there further performance gains by using an array in multiple dimensions? Also, when memory is dynamically allocated for a 2d array, are they stored in contiguous locations, or are they scattered around the heap? When the data is requested, can I assume that everything is pulled from memory as a block?
Most of the information that I have seen has just explained syntax. Any answers or suggested reading would be great.
It can, and in fact I would recommend this in the general case.
Not really. Depending on your layout you could optimize cache hits, but then precisely the same is true with the flattened 1D array. The memory layout between the two is (usually) precisely the same. The only difference is the semantic type of the array and the fact that you now have to implement the 2D element lookup yourself.
Arrays are always contiguous.
You should be careful though that you’re actually allocating a 2D array. Some people write
int** ptr = new int*[2]then allocate each “sub-array” manually and think that they have a 2D array. They do not. They have an array of pointers, and that’s when you get your “scattered” layout. Your 2D array isint (*ptr)[3] = new int[2][3];.