静态数组定义为未指定大小、空括号?
对于下面的 C++ 代码片段:
class Foo {
int a[]; // no error
};
int a[]; // error: storage size of 'a' isn't known
void bar() {
int a[]; // error: storage size of 'a' isn't known
}
为什么成员变量也没有导致错误?这个成员变量的含义是什么?
我通过 CodeBlocks 8.02 使用 gcc 版本 3.4.5(mingw-vista 专用)。
在 Visual Studio Express 2008 - Microsoft(R) C/C++ Optimizing Compiler 15.00.30729.01 for 80x86 上,我收到以下消息:
class Foo {
int a[]; // warning C4200: nonstandard extension used : zero-sized array in struct/union - Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
};
int a[];
void bar() {
int a[]; // error C2133: 'a' : unknown size
}
现在,这也需要一些解释。
For the C++ code fragment below:
class Foo {
int a[]; // no error
};
int a[]; // error: storage size of 'a' isn't known
void bar() {
int a[]; // error: storage size of 'a' isn't known
}
why isn't the member variable causing an error too? and what is the meaning of this member variable?
I'm using gcc version 3.4.5 (mingw-vista special) through CodeBlocks 8.02.
On Visual Studio Express 2008 - Microsoft(R) C/C++ Optimizing Compiler 15.00.30729.01 for 80x86, I got the following messages:
class Foo {
int a[]; // warning C4200: nonstandard extension used : zero-sized array in struct/union - Cannot generate copy-ctor or copy-assignment operator when UDT contains a zero-sized array
};
int a[];
void bar() {
int a[]; // error C2133: 'a' : unknown size
}
Now, this needs some explaination too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
C++ 语言仅允许在非定义声明中省略数组大小。
非静态类成员声明始终需要指定数组大小
,而静态类成员声明可以省略大小
(当然,同一成员的定义必须指定大小)。
与此行为的任何偏差只能通过编译器的扩展非标准行为来解释。也许您应该指定一些额外的编译器设置,以使其表现得更加迂腐。
C++ language allows omitting array size only in non-defining declarations
Non-static class member decarations are always required to specify the array size
while static class member decarations can omit the size
(the definition of the same member will, of course, have to specify the size).
Any deviations from this behavior can only be explaind by extended non-standard behavior of your compiler. Maybe you should specify some additional compiler settings to make it behave in more pedantic fashion.
C99 支持所谓的“灵活”数组成员,它允许作为结构的最后一个成员。当您动态分配此类结构时,您可以增加从
malloc()
请求的数量,以为数组提供内存。一些编译器将其添加为 C90 和/或 C++ 的扩展。
因此,您可以使用如下代码:
您不能直接定义具有灵活数组成员的结构(作为本地变量或全局/静态变量),因为编译器不知道要为其分配多少内存。
老实说,我不确定如何通过 C++ 的 new 运算符轻松使用这样的东西 - 我认为您必须使用 malloc() 为对象分配内存> 并使用放置
new
。也许可以使用operator new
的某些类/结构特定重载......C99 supports something called a 'flexible' array member that is allowed to be the last member of a struct. When you dynamically allocate such a struct you can increase the amount requested from
malloc()
to provide for memory for the array.Some compilers add this as an extension to C90 and/or C++.
So you can have code like the following:
You can't define a struct with a flexible array member directly (as a local or a global/static variable) as the compiler won't know how much memory to allocate for it.
I'm honestly not sure how you'd easily use such a thing with C++'s
new
operator - I think you'd have to allocate the memory for the object usingmalloc()
and use placementnew
. Maybe some class/struct specific overload ofoperator new
could be used...未指定大小的数组类型是不完整的。 8.3.4/1:
它必须完成才能参与定义,即
a
的定义必须包含大小规范或具有指定大小的数组的初始化。An array type with unspecified size is incomplete. 8.3.4/1:
It must be completed in order to participate in a definition, ie the definition of
a
must contain a size specification or initialization with an array of specified size.我们用它来表示某种可变长度记录。类似头文件的东西,其中包含有关要遵循多少个结构的信息,后面是数据本身。它是一个可变长度数组,我发现编译器之间并没有很好地支持它。有些想要 array[];有些想要 array[0]; (旧样式)。
We've used this to denote a variable length record of some sort. Something like a header file that has information on how many structures to follow, followed by the data itself. It's a variable length array and I've found it is not supported well between compilers. Some want array[]; and some want array[0]; (old style).