Column-major array storage in C compilers

◇◆丶佛笑我妖孽 提交于 2019-12-10 10:55:43

问题


Are there any C compilers that have extensions to store an array in column-major order instead of the standard row-major order?


回答1:


Short answer is "No".

Long answer is that storing an array in column-major order would break the one-to-one correspondence between array index operations and pointer arithmetics, and the way an N-dimension array is sliced into N-1 dimension arrays.

Consider a 10x20 array stored in column-major order. This means that cells in adjacent columns would be next to each other in memory. On the other hand, converting a pointer to array element at i, j to an element pointer must work like this:

int *p=&a[1][5];
int *q=&a[1][6];
p++;

The standard requires that p is equal q, because the two pointers point to adjacent elements. This would not be possible if array a were stored in column-major order.

In C you would have to write your own set of functions to work with such arrays. If you code in C++, however, you would have an option to implement your own multi-dimension array, and overload the parentheses operator () to work in a column-major order.



来源:https://stackoverflow.com/questions/32160943/column-major-array-storage-in-c-compilers

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