零长度数组声明 [英] Zero length array declaration

查看:64
本文介绍了零长度数组声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




想看下面的构造是否有效:

typedef struct {

int foo;

char bar [0];

} foobar;


基本上,我们的想法是让上面的结构指向一条消息

缓冲区,它有一个4字节的整数,后跟一个变量流

字节数。结构构件bar是指结构构件。用于引用

字节流。上面的代码编译很好,在Solaris上运行的基于GNU的PPC

交叉编译器似乎可以实现

的功能。


我的问题是:声明一个长度为零的数组是否合法?或者

栏应该被宣布为至少一个元素长度?


评论赞赏。


RS

解决方案

RS写道:



想看看下面的构造是有效的:
typedef struct {
int foo;
char bar [0];
} foobar;

基本上,我的想法是上面的结构指向一个消息
缓冲区,它有一个4字节的整数,后跟一个变量的字节数。结构构件bar是指结构构件。用于引用
字节流。上面的代码在Solaris上运行的基于GNU的PPC
交叉编译器编译得很好,并且好像打算使用

我的问题是:声明一个是否合法长度为零的数组?或者
栏应该被宣布为至少一个元素长度?




所写的构造不是合法的C在
$下b $ b版本的标准,虽然有些编译器可能会允许它作为语言的扩展名。


将'bar''声明为一个 - 元素数组是并且始终具有合法的
。但是,为结构分配额外的内存

,然后使用bar,就像它有多个元素一样,

不是。这种特殊的滥用(称为结构黑客)对绝大多数C实现都有效,但实际上并不是合法的。


最新的C99标准的版本使

struct hack合法化,但引入了一种新的语法:你声明`bar''

完全没有数组大小,如`char bar [] ''。然而,这仍然是相对较新的,而且还没有被可用的

编译器广泛支持。


-
Er*********@sun.com


>


想看看以下构造是否有效:
typedef struct {
int foo;
char bar [0];
} foobar;



我不确定这是否合法 - 其他人可以回答这个问题。


出于上述目的,这是错误的。


char bar [0]应该是char * bar而不是。这就是

指针的用途 - 引用或指向位于其他地方的某个存储空间



< blockquote> jj******@yahoo.com (jjr2004a)写道:

查看以下构造是否有效:
typedef struct {
int foo;
char bar [0];
} foobar;


我不确定这个的合法性 - 其他人可以回答这个问题。

出于声明的目的,这是错误的。

char bar [0]应该是char * bar而不是。这就是
指针的用途 - 引用或指向位于其他地方的某个存储空间。




不,重点是将bar声明为数组是指数组(某些

动态大小)存储在结构本身中。结构是通过调用malloc来分配的,其大小等于

结构本身的大小加上要存储在
$ b中的字符数。 $ b数组。它被称为struct hack;它通常被支持,并且

常用,但不是严格合法的。


声明char * bar当然也是有效的,但它是不同的

的东西,需要单独的内存分配(以及后来的释放)

的数组。


正如其他人所说的那样,C99增加了对struct hack的支持,

但是使用了新的语法。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti .net / ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


Hi,

Looking to see if the following construct is valid:
typedef struct {
int foo;
char bar[0];
} foobar;

Basically, the idea is to have the structure above point to a message
buffer that has a 4-byte integer followed by a stream of variable
number of bytes. The structure member "bar" is used to reference the
stream of bytes. The above code compiles fine with a GNU based PPC
cross-compiler running on Solaris and seems to do the function
intended.

My question is: Is it legal to declare an array with zero length? Or
should bar have been declared to be at least one element in length?

Comments appreciated.

RS

解决方案

RS wrote:

Hi,

Looking to see if the following construct is valid:
typedef struct {
int foo;
char bar[0];
} foobar;

Basically, the idea is to have the structure above point to a message
buffer that has a 4-byte integer followed by a stream of variable
number of bytes. The structure member "bar" is used to reference the
stream of bytes. The above code compiles fine with a GNU based PPC
cross-compiler running on Solaris and seems to do the function
intended.

My question is: Is it legal to declare an array with zero length? Or
should bar have been declared to be at least one element in length?



The construct as written is not legal C under either
version of the Standard, although some compilers may allow
it as an extension to the language.

Declaring `bar'' as a one-element array is and always has
been legal. However, allocating extra memory for the struct
and then using `bar'' as if it had more than one element is
not. This particular abuse (known as "the struct hack") works
on the great majority of C implementations, but is not actually
legitimate.

The latest "C99" version of the Standard legitimizes the
struct hack, but introduces a new syntax: you declare `bar''
with no array size at all, as `char bar[]''. However, this is
still relatively new and not yet widely supported by available
compilers.

--
Er*********@sun.com


> Hi,


Looking to see if the following construct is valid:
typedef struct {
int foo;
char bar[0];
} foobar;


I not sure about the legality of this - others can answer that.

For the stated purpose this is wrong.

char bar[0] should be char *bar instead. That''s what a
pointer is for - to refer or point to some storage located
somewhere else.


jj******@yahoo.com (jjr2004a) writes:

Looking to see if the following construct is valid:
typedef struct {
int foo;
char bar[0];
} foobar;


I not sure about the legality of this - others can answer that.

For the stated purpose this is wrong.

char bar[0] should be char *bar instead. That''s what a
pointer is for - to refer or point to some storage located
somewhere else.



No, the point of declaring bar as an array is that the array (of some
dynamic size) is stored within the struct itself. The structure is
allocated by calling malloc with a size equal to the size of the
structure itself plus the number of characters to be stored in the
array. It''s called the "struct hack"; it''s commonly supported, and
commonly used, but not strictly legal.

Declaring "char *bar" is of course valid as well, but it''s a different
thing, requiring a separate memory allocation (and later deallocation)
for the array.

As others have said elsethread, C99 adds support for the struct hack,
but with a new syntax.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于零长度数组声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆