C++ 大小为 0 的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6180012/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:38:36  来源:igfitidea点击:

Array with size 0

c++carraysgcc

提问by zw324

Today I incidentally defined a two dimensional array with the size of one dimension being 0, however my compiler did not complain. I found the following which states that this is legal, at least in the case of gcc:

今天我顺便定义了一个二维数组,一维的大小为0,但是我的编译器没有抱怨。我发现以下内容表明这是合法的,至少在 gcc 的情况下:

6.17 Arrays of Length Zero

6.17 长度为零的数组

However, I have two questions on this usage:

但是,我对这种用法有两个疑问:

First, is this considered as good programming practice? If so, then when should we use it in real world?

首先,这被认为是良好的编程习惯吗?如果是这样,那么我们什么时候应该在现实世界中使用它?

Second, the array I defined was two dimensional, with 0 size for one dimension. Is this the same as the one dimensional case? For example,

其次,我定义的数组是二维的,一维大小为 0。这和一维情况一样吗?例如,

int s[0]
int s[0][100]
int s[100][0]

Are they all the same in the memory and for the compiler?

它们在内存和编译器中都是一样的吗?

EDIT: Reply to Greg: The compiler I am using is gcc 4.4.5. My intention for this problem is not compiler-dependent, however if there are any compiler specific quirks that would be helpful too:)

编辑:回复 Greg:我使用的编译器是 gcc 4.4.5。我对这个问题的意图不依赖于编译器,但是如果有任何特定于编译器的怪癖也会有帮助:)

Thanks in advance!

提前致谢!

采纳答案by CB Bailey

In C++ it is illegal to declare an array of zero length. As such it is not normally considered a good practice as you are tying your code to a particular compiler extension. Many uses of dynamically sized arrays are better replaced with a container class such as std::vector.

在 C++ 中,声明长度为零的数组是非法的。因此,当您将代码绑定到特定的编译器扩展时,它通常不被认为是一种好的做法。动态大小数组的许多用途最好替换为容器类,例如std::vector.

ISO/IEC 14882:2003 8.3.4/1:

ISO/IEC 14882:2003 8.3.4/1:

If the constant-expression(5.19) is present, it shall be an integral constant expression and its value shall be greater than zero.

如果常量表达式(5.19) 存在,它应该是一个整数常量表达式并且它的值应该大于零。

However, you can dynamically allocate an array of zero length with new[].

但是,您可以使用 动态分配零长度的数组new[]

ISO/IEC 14882:2003 5.3.4/6:

ISO/IEC 14882:2003 5.3.4/6:

The expression in a direct-new-declaratorshall have integral or enumeration type (3.9.1) with a non-negative value.

direct-new-declarator 中的表达式应具有非负值的整数或枚举类型 (3.9.1)。

回答by Node

I ran this program at ideone.com

我在ideone.com 上运行了这个程序

#include <iostream>

int main()
{
    int a[0];
    int b[0][100];
    int c[100][0];

    std::cout << "sizeof(a) = " << sizeof(a) << std::endl;
    std::cout << "sizeof(b) = " << sizeof(b) << std::endl;
    std::cout << "sizeof(c) = " << sizeof(c) << std::endl;

    return 0;
}

It gave the size of all the variables as 0.

它将所有变量的大小设为 0。

sizeof(a) = 0
sizeof(b) = 0
sizeof(c) = 0

So in the above example, no memory is allocated for a, bor c.

所以在上面的例子中,没有为a,b或分配内存c

回答by Kanopus

Compiling your example with gcc, all three of them have sizeof0, so I would assume that all of them are treated equally by the compiler.

用 gcc 编译你的例子,它们三个都有sizeof0,所以我假设所有这些都被编译器同等对待。

回答by Piotr Praszmo

Your link explains everything. They are used as last field in a struct when the length of struct is not known at compile time. If you try using them on stack or in a middle of other declarations you will end up overwriting next elements.

您的链接说明了一切。当编译时不知道结构体的长度时,它们用作结构体中的最后一个字段。如果您尝试在堆栈上或在其他声明的中间使用它们,您最终将覆盖下一个元素。