n 维(n>=2)数组在内存中如何表示?
Can anyone provide me with a formula so that I can understand the memory representation of an n-dimensional(n>=2) array like this "How_are_two-dimensional_arrays_represented_in_memory"?
This calculation is applicable for 2D-arrays only.
How to calculate, suppose, a 5D array?
Ok....
I think I found the answer: Array_data_structure#Two-dimensional_arrays
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
C 中的二维数组只不过是数组的数组。 3 维数组是数组的数组的数组。等等。
C99 标准的相关部分是 6.5 .2.1、“数组下标”:
索引运算符是根据指针算术定义的这一事实引起了一些混乱。这并不意味着数组是“真正的指针”——事实上它们绝对不是。声明数组对象根本不会创建任何指针对象(当然除非它是指针数组)。但是引用数组的表达式通常(但并非总是)“衰减”为指向数组第一个元素的指针(这是一个指针值,而不是指针对象) 。
现在,简单的数组对象,无论维度如何,都非常不灵活。在 C99 之前,所有数组对象都必须具有在编译时确定的固定大小。 C99 引入了可变长度数组 (VLA),但即便如此,VLA 的大小在声明时也是固定的(并且并非所有编译器都支持 VLA,即使在 C99 标准发布 12 年后也是如此)。
如果您需要更灵活的东西,常见的方法是声明一个指向元素类型的指针,然后使用 malloc() 分配一个数组并使指针指向数组的第一个元素:
这使您可以使用与声明的固定大小数组对象相同的语法引用堆分配数组的元素,但在
arr[i]
中,表达式arr
衰减到一个指针,而在ptr[i]
`ptr已经是一个指针。同样的事情可以扩展到更高的维度。您可以分配一个指针数组,然后初始化每个指针以指向所分配的数组的开头无论。
这给你提供了一些行为非常类似于二维(或更多)数组的东西,但是你必须自己管理内存;这就是更大灵活性的代价。
严格来说,这不是二维数组。正如我上面所说,二维数组只是数组的数组。将其视为二维数组可能并非完全不合理,但这与 C 标准中的用法相冲突;它类似于将链表称为一维数组。
comp.lang.c FAQ 是一个很好的资源;第 6 节介绍了数组和指针,特别出色。
A 2-dimensional array in C is nothing more or less than an array of arrays. A 3-dimensional array is an array of arrays of arrays. And so on.
The relevant section from the C99 standard is 6.5.2.1, "Array subscripting":
Some confusion is caused by the fact that the indexing operator is defined in terms of pointer arithmetic. This does not imply that arrays are "really pointers" -- and in fact they very definitely are not. Declaring an array object does not create any pointer objects at all (unless of course it's an array of pointers). But an expression that refers to the array usually (but not always) "decays" to a pointer to the array's first element (that's a pointer value, not a pointer object).
Now simple array objects, of however many dimensions, are quite inflexible. Prior to C99, all array objects had to be of a fixed size determined at compile time. C99 introduced variable-length arrays (VLAs), but even so a VLA's size is fixed when it's declared (and not all compilers support VLAs, even 12 years after the C99 standard was issued).
If you need something more flexible, a common approach is to declare a pointer to the element type, and then allocate an array using
malloc()
and have the pointer point to the array's first element:This lets you refer to elements of the heap-allocated array using the same syntax you'd use for a declared fixed-size array object, but in
arr[i]
the expressionarr
decays to a pointer, whereas inptr[i]
`ptr is already a pointer.The same thing can be extended to higher dimensions. You can allocate an array of pointers, and then initialize each pointer to point to the beginning of an allocated array of whatever.
This gives you something that acts very much like a 2-dimensional (or more) array, but you have to manage the memory yourself; that's the price of the greater flexibility.
Strictly speaking, this is not a 2-dimensional array. A 2-dimensional array, as I said above, is only an array of arrays. It's probably not entirely unreasonable to think of it as a 2-D array, but that conflicts with the usage in the C Standard; it's similar to referring to a linked list as a 1-D array.
The comp.lang.c FAQ is a good resource; section 6, which covers arrays and pointers, is particularly excellent.
二维数组实际上是指向数组的指针的数组。二维整数数组
a[i][j]
将占用i*sizeof(int*)
作为指针数组,而i* j*sizeof(int)
用于最终数组。3-D 数组
a[i1][i2][i3]
是指向数组的指针数组的指针数组。第一层数组包含i1
指针,第二层包含i1*i2
指针,第三层包含i1*i2*i3
整数。一般来说,大小为
i1..iN
的 N 维数组将具有N-1
层指针数组和 1 层整数数组。 N 级数组的长度为iN
,并且该级别中有 i1..iN-1 数组的乘积。
所以,一个 5 维数组:
希望有帮助(我希望我的索引是正确的)。
您发布的维基百科链接引用了/不同类型的多维数组/。默认情况下,C 多维数组就是我刚才描述的方式。您还可以将它们抽象为一维数组。这节省了内存并使整个数组连续,但它使访问元素变得更加复杂。对于 5 维示例:
每个项都有一个偏移量乘以我们需要偏移的元素数。例如,要增加一个第一维度级别,我们需要遍历剩余的四个维度一次以“环绕”(如果您愿意的话)。
A 2 dimensional array is really an array of pointers to arrays. A 2-dimensional array of integers
a[i][j]
will take upi*sizeof(int*)
for the array of pointers, andi*j*sizeof(int)
for the final array.A 3-D array
a[i1][i2][i3]
is an array of pointers to arrays of pointers to arrays. The first level of arrays containsi1
pointers, the second level containsi1*i2
pointers, the third level containsi1*i2*i3
integers.In general, an N-dimensional array with sizes
i1..iN
will haveN-1
levels of arrays of pointers and 1 level of arrays of ints. The arrays in level N have lengthiN
and there areproduct of i1..iN-1
arrays in that level.So, a 5-D array:
Hope that helps (and I hope I got the indices right).
That wikipedia link you posted refers to a /different kind of multidimensional array/. By default, C multidimensional arrays are the way I just described. You can also abstract them as a single dimensional array. This saves memory and makes the entire array contiguous, but it makes accessing elements somewhat more complex. For the 5-D example:
each term there is an offset times the number of elements we need to offset. For example, to increment by one first-dimension level we need to traverse the the four remaining dimensions once to 'wrap around', if you will.
我记得,C 语言中数组在内存中的存储方式尚未标准化。但有关数组以及它们如何存储在内存中的一些信息,请参阅以下两个链接:
http://webster.cs.ucr.edu/AoA/Windows/HTML/Arraysa2.html
http://publications.gbdirect.co.uk/c_book/chapter5/arrays.html
第一个链接更通用,讨论存储数组的不同方式,而第二个链接讨论 C 数组在内存中布局的最可能方式。
How arrays are stored in memory for C is not, as I recall, standardized. But for some information about arrays, and how they might be stored in memory, see the following two links:
http://webster.cs.ucr.edu/AoA/Windows/HTML/Arraysa2.html
http://publications.gbdirect.co.uk/c_book/chapter5/arrays.html
The first link is more general, and discusses different ways of storing arrays, while the second discusses the most likely way a C array may be layout in memory.