数组大小为零! [英] Array of size zero!

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

问题描述

大家好,


我看过一段代码(在进行代码审查时),它声明了一个大小为0的

数组。朋友告诉虽然它不标准

C,有些编译器会支持这个......我很好奇知道使用

吧..

代码是使用Diab C编译器编译的。

此外,数组也在这样的结构中声明


typedef struct someStruct

{

int i;

char array [0];

} someStruct;

谢谢

问候

解决方案

标准的写法是:

typedef struct someStruct

{

int i;

char array [];

} someStruct;


没有零。

这是一个灵活的数组,由C99支持。


你分配的结构大于sizeof(int),

分配数组的大小

" prashna" < VA ****** @ rediffmail.com>在留言中写道

news:d4 ************************** @ posting.google.c om ...

大家好,

我看过一段代码(在进行代码审查时),它声明了一个大小为0的数组。我的一位朋友告诉我它不是标准的,有些编译器会支持这个......我非常好奇知道它的用法..
代码是用Diab C编译器编译的。
此外,数组在结构中被声明为

typedef struct someStruct
{
int i;
char array [0];
} someStruct;

谢谢
关心




我认为这个C99变种是char array [];。

零长度数组的目的是为了解决分配可变长度结构的

问题,其中

声明的部分该结构代表一个固定长度的

"标题"在运行时计算可变长度部分的大小是




有些编译器有非标准扩展允许

指定的数组长度为零,这与C99省略的长度规范具有相同的效果




对于不能编译的编译器支持任何规范,

臭名昭着的struct hack和使用技术将数组

长度指定为1,并且

可变长度结构的大小计算考虑到

存在结构长度中数组的一个元素

由sizeof()返回。


jacob navia写道:

标准的写作方式是:
typedef struct someStruct
{i /;
char array [];
} someStruct;
这是一个灵活的数组,并由C99支持。

你分配的结构超过sizeof(int),以分配大小数组




我认为这是不正确的,因为它假设

array []在i之后启动。正如常见问题解答告诉我们的那样,你应该使用offsetof()宏,它可以使用
来计算someStruct中数组的字节偏移量。

该宏应在< stddef.h>中定义。如果没有,

使用FAQ中的#definition。


案例


Hi all,

I have seen a piece of code(while doing code review) which declared an
array of size 0.One of my friend told although it is not standard
C,some compilers will support this..I am very curious to know the use
of it..
The code was compiled using Diab C compiler.
Also the array was declared in structure like this

typedef struct someStruct
{
int i;
char array[0];
} someStruct;
Thanks
Regards

解决方案

The standard way of writing this is:
typedef struct someStruct
{
int i;
char array[];
} someStruct;

Without the zero.
This is a flexible array and is supported by C99.

you allocate the structure with more than sizeof(int), to
allocate the size of the array


"prashna" <va******@rediffmail.com> wrote in message
news:d4**************************@posting.google.c om...

Hi all,

I have seen a piece of code(while doing code review) which declared an
array of size 0.One of my friend told although it is not standard
C,some compilers will support this..I am very curious to know the use
of it..
The code was compiled using Diab C compiler.
Also the array was declared in structure like this

typedef struct someStruct
{
int i;
char array[0];
} someStruct;
Thanks
Regards



I think the C99 variant of this is "char array[];". The
zero-length array thing is intended to get around the
problem of allocating a variable-length structure, where
the declared portion of the structure represents a fixed-length
"header" and the size of the variable-length portion is
calculated at run-time.

Some compilers have a non-standard extension that allows
an array length of zero specified, which is the same effect
as the C99 elided length specification.

For compilers that don''t support either specification, the
infamous "struct hack" technique is used where the array
length is specified as 1, and size calculations for the
variable-length structure take into consideration the
presence of one element of the array in the structure length
returned by sizeof().


jacob navia wrote:

The standard way of writing this is:
typedef struct someStruct
{
int i;
char array[];
} someStruct;

Without the zero.
This is a flexible array and is supported by C99.

you allocate the structure with more than sizeof(int), to
allocate the size of the array



I geuss this is not correct because it assumes that
array[] starts right after i. As the FAQ tells us,
you should use the offsetof() macro, which can
calculate the byte offset of array in someStruct.
This macro should be defined in <stddef.h>. If not,
use the #definition in the FAQ.

Case


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

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