静态多维数组如何分配内存?
所有,
这已经困扰我一段时间了。在 C\C++(我猜还有 java 和 .NET)中,我们不必在多维数组中指定行索引。 因此,例如我可以声明一个整数数组,如下所示:
int Array[][100];
我认为静态数组通常表示为堆栈上的连续内存。那么,采用列主表示形式,编译器如何知道在上述情况下由于缺少其中一个维度而要分配多少内存?
All,
This has been bugging me for a while now. In C\C++( i guess java and .NET as well) we do not have to specify the row index in a multi-dimensional array.
So, for example i can declare an array of ints as such:
int Array[][100];
I think static arrays in general are represented as contiguous memory on the stack. So, taking a column-major representation, how does the compiler know how much memory to allocate in the above case as it's missing one of the dimensions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在 C++ 语言中,你不能这样做,
因为这将是不完整类型对象的定义,这在 C++ 中是明确非法的。您可以在非定义声明中使用它
(或作为类的静态成员),但是当涉及到同一数组对象的实际定义时,必须显式指定两个大小(或从显式初始化程序派生) )。
在 C 中,情况并没有太大不同,只是在 C 中,有诸如“暂定定义”之类的东西,可以让您编写
然而,在这方面,暂定定义与非定义声明非常相似,其中这就是为什么它被允许。最终,您将必须在同一翻译单元中定义具有显式指定大小的同一对象(某些编译器不要求将其作为非标准扩展)。如果你在非暂定定义中尝试类似的东西,你会得到一个错误
所以,如果你想一想,除了暂定定义之外,C 和 C++ 中的情况没有太大不同:定义以下对象是非法的:这些语言中的不完整类型和未指定大小的数组是不完整类型。
In C++ language you can't just do
because that would be a definition of an object of incomplete type, which is explicitly illegal in C++. You can use that in a non-defining declaration
(or as a static member of a class), but when it will come to the actual definition of the same array object both sizes will have to be specified explicitly (or derived from an explicit initializer).
In C the situation is not much different, except that in C there are such things as tentative definitions which let you write
However, a tentative definition in this regard is pretty similar to a non-defining declaration, which is why it is allowed. Eventually you will have to define the same object with explicitly specified size in the same translation unit (some compilers don't require that as an non-stanard extension). If you try something like that in a non-tentative definition, you'll get an error
So, if you think of it, aside from tentative definitions, the situation in C and C++ is not much different: it is illegal to define objects of incomplete type in these languages and an array of unspecified size is an incomplete type.
在 java 和 .NET 中,不要考虑“堆栈”——对象存在于堆上。在 C 中,这只是一个声明——只有定义实际上保留了内存!因此,这不是一个可接受的定义 - 如果您将其作为文件
ac
中的唯一行:那么 gcc 只是将其视为
int Array[1][100] ;
,因为它警告您它正在做的事情。In java and .NET, don't think about "the stack" -- objects live on the heap. And in C, that's just a declaration -- only a definition actually reserves memory! So that would NOT be an acceptable definition -- if you put it as the only line in file
a.c
:so gcc is just treating it as if it were
int Array[1][100];
, as it warns you it's doing.它不知道要分配多少内存,他知道
array[]
是数组是一个指针(如int *array
)。array[][100]
(如果我错了,请纠正我)与array[100]
相同。It does not know how much memory to allocate, what he knows with
array[]
is that array is a pointer (likeint *array
).array[][100]
( someone please correct me if i am wrong ) is the same asarray[100]
.