灵活的数组成员+可变长度数组 [英] Flexible array member + variable length array

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

问题描述

大家好,


这个结构记录指针数组的长度为

第一个成员:

struct array {

ptrdiff_t length;

void * ptr [];

};


如何使用可变长度数组初始化此结构?


ptrdiff_t len = 10;

{

struct array a [len + 1];

...

}


看来上面的代码只能工作为len指针预留空间

如果sizeof长度与指向void的指针的sizeof相同,那么

长度与指向void之间没有填充。是否有我忽略的便携式C99

语法? (或者我是否必须创建一个类型为

void *的VLA并继续将第一个参数转换为ptrdiff_t?)


谢谢,

Adam

Hi all,

With this structure that records the length of an array of pointers as its
first member:

struct array {
ptrdiff_t length;
void *ptr[];
};

How does one initialise this structure using a variable length array?

ptrdiff_t len=10;
{
struct array a[len+1];
...
}

It appears the above code will only work to reserve space for len pointers
if the sizeof length is the same as sizeof pointers to void and there is
no padding between length and the pointers to void. Is there portable C99
syntax that I have overlooked? (or will I have to create a VLA of type
void * and keep casting the first argument to ptrdiff_t?)

Thanks,
Adam

推荐答案

Adam Warner写道:
Adam Warner wrote:
大家好,

这个记录指针数组长度的结构作为其第一个成员:

struct array {
ptrdiff_t length;
void * ptr [];
};

如何使用可变长度数组初始化此结构?

ptrdiff_t len = 10;
{struct struct a [len +1];
...

看来上面的代码只能为len指针保留空间
如果sizeof长度是相同的sizeof指向void,并且长度和指向void之间没有填充。是否有我忽略的便携式C99
语法? (或者我是否必须创建一个类型为
void *的VLA并继续将第一个参数转换为ptrdiff_t?)
Hi all,

With this structure that records the length of an array of pointers as its
first member:

struct array {
ptrdiff_t length;
void *ptr[];
};

How does one initialise this structure using a variable length array?

ptrdiff_t len=10;
{
struct array a[len+1];
...
}

It appears the above code will only work to reserve space for len pointers
if the sizeof length is the same as sizeof pointers to void and there is
no padding between length and the pointers to void. Is there portable C99
syntax that I have overlooked? (or will I have to create a VLA of type
void * and keep casting the first argument to ptrdiff_t?)




我不确定你是什么要求。标准

的例子如下:


(6.7.2.1)

"

17

示例假设所有数组成员对齐相同,在

声明之后:

struct s {int n; double d []; };

struct ss {int n;双d [1];
三个表达式:

sizeof(struct s)

offsetof(struct s,d)

offsetof(struct ss,d)

具有相同的值。结构struct有一个灵活的阵列成员

d。


18

如果sizeof(double)是8,那么之后执行以下代码:

struct s * s1;

struct s * s2;

s1 = malloc(sizeof(struct s)+ 64);

s2 = malloc(sizeof(struct s)+ 46);

并假设对malloc的调用成功,
指向的对象
s1和s2的行为就好像标识符已被声明为:

struct {int n;双d [8]; } * s1;

struct {int n;双d [5]; } * s2;

"


具有灵活数组成员的结构当然不能将
放入数组中。

干杯

Michael

-

电子邮件:我的是/ at / gmx / dot / de地址。



I am not sure what you are asking for. The example from the standard
runs like this:

(6.7.2.1)
"
17
EXAMPLE Assuming that all array members are aligned the same, after the
declarations:
struct s { int n; double d[]; };
struct ss { int n; double d[1]; };
the three expressions:
sizeof (struct s)
offsetof(struct s, d)
offsetof(struct ss, d)
have the same value. The structure struct s has a flexible array member
d.

18
If sizeof (double) is 8, then after the following code is executed:
struct s *s1;
struct s *s2;
s1 = malloc(sizeof (struct s) + 64);
s2 = malloc(sizeof (struct s) + 46);
and assuming that the calls to malloc succeed, the objects pointed to by
s1 and s2 behave as if the identifiers had been declared as:
struct { int n; double d[8]; } *s1;
struct { int n; double d[5]; } *s2;
"

Structures with flexible array members of course cannot be
put into arrays.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


访问此页面:

http://gcc.gnu.org/ml/gcc/2002-04/msg00026.html


希望它有所帮助。


干杯

Shan


Adam Warner写道:
Visit this page:

http://gcc.gnu.org/ml/gcc/2002-04/msg00026.html

Hope it helps.

Cheers
Shan

Adam Warner wrote:

这个结构记录了一个指针数组的长度
作为它的第一个成员:

struct array {
ptrdiff_t长度;
void * ptr [];
};

如何使用可变长度数组初始化此结构?

ptrdiff_t len = 10 ;
{struct struct a [len + 1];
...


看来上面的鳕鱼如果sizeof长度与sizeof指针的sizeof相同,并且
在长度和指向void之间没有填充,那么e只会为len
指针保留空间。是否有我忽略的便携式
C99语法? (或者我是否必须创建
类型的VLA *并继续将第一个参数转换为ptrdiff_t?)

谢谢,
Adam
Hi all,

With this structure that records the length of an array of pointers as its first member:

struct array {
ptrdiff_t length;
void *ptr[];
};

How does one initialise this structure using a variable length array?

ptrdiff_t len=10;
{
struct array a[len+1];
...
}

It appears the above code will only work to reserve space for len pointers if the sizeof length is the same as sizeof pointers to void and there is no padding between length and the pointers to void. Is there portable C99 syntax that I have overlooked? (or will I have to create a VLA of type void * and keep casting the first argument to ptrdiff_t?)

Thanks,
Adam








Shan写道:

Shan wrote:
访问此页:

http://gcc.gnu.org/ml/gcc/2002- 04 / msg00026.html

希望有所帮助。

干杯
Shan
Adam Warner写道:
Visit this page:

http://gcc.gnu.org/ml/gcc/2002-04/msg00026.html

Hope it helps.

Cheers
Shan

Adam Warner wrote:
大家好,

这个结构记录了一个指针数组的长度为
Hi all,

With this structure that records the length of an array of pointers as its
第一个成员:

struct array {
ptrdiff_t length;
void * ptr [];
};

如何使用可变长度
数组初始化此结构?
ptrdiff_t len = 10;
{struct struct a [len + 1];
...


看来上面代码只能用于保留sp len for len
first member:

struct array {
ptrdiff_t length;
void *ptr[];
};

How does one initialise this structure using a variable length array?
ptrdiff_t len=10;
{
struct array a[len+1];
...
}

It appears the above code will only work to reserve space for len


指针

如果sizeof长度与指向void的sizeof指针相同,并且
if the sizeof length is the same as sizeof pointers to void and



那里是


there is

长度和指向void之间没有填充。是否
no padding between length and the pointers to void. Is there


可移植的C99

portable C99

我忽略了什么语法? (或者我是否必须创建一个
syntax that I have overlooked? (or will I have to create a VLA of


类型

void *并继续将第一个参数转换为ptrdiff_t?)

谢谢,<亚当
void * and keep casting the first argument to ptrdiff_t?)

Thanks,
Adam




抱歉,我错误地发布了....



Sorry, i top posted by mistake....

这篇关于灵活的数组成员+可变长度数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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