What is the purpose of multidimensional arrays? [closed]

微笑、不失礼 提交于 2019-12-10 23:17:43

问题


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.


回答1:


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]?

It can, and in fact I would recommend this in the general case.

Are there further performance gains by using an array in multiple dimensions?

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.

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?

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 is int (*ptr)[3] = new int[2][3];.




回答2:


First at all, there a multidimensional problems. Secondly, if the multidimensional problem is 'sparse' it doesn't make sense to allocate 99*99*99*99*...*99 elements, but only pointers to next level structures, which are hidden by the clever syntax array[n][i][j][k]...

Virtual memory and page tables for example operate that way in modern OS's and CPUs.




回答3:


There are two types of 2d arrays:

1) the kind where all the memory is in one big block, and

2) the kind where every row (or every column) is contiguous but the individual rows are scattered around, and you have an array of pointers to point to each row.

Type #2 has better performance in case you want to do certain things like swapping rows, because all you have to do is swap two pointers.



来源:https://stackoverflow.com/questions/13273928/what-is-the-purpose-of-multidimensional-arrays

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!