C90中的可变长度结构 [英] variable length structures in C90

查看:153
本文介绍了C90中的可变长度结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GNU C中允许使用零长度数组
,因此可以进行初始化
$ b

  struct line {
int长度;
char内容[0];
};

struct line * thisline =(struct line *)
malloc(sizeof(struct line)+ this_length);
thisline-> length = this_length;

注意:我在这里指的是 http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
(提供C中的变长结构的基本介绍)

它继续说:在ISO C90中,你必须给内容长度为1,这意味着要么你浪费空间或使malloc参数复杂化。



这是什么意思?有人可以提供一个例子说明如何在C90中初始化变长结构以帮助理解吗?

解决方案

如果您确实需要使用c90,那么问题2.6 涵盖了 C FAQ 。 :

  struct name {
int namelen;
char namestr [1];
};

struct name * ret =
malloc(sizeof(struct name)-1 + strlen(newname)+1);初始[1]为
/ * -1; +1为\0 * /

虽然常见问题的答案是:


目前还不清楚它是合法的还是便携式的,但它很受欢迎。

尽管 gcc 这个技术的实现可能看起来像这样。文件基本上说他们支持它,在C99中,常见问题解答表示他们添加了灵活数组成员,我在这个答案中涵盖了,它在 6.7.2.1 结构和联合说明符部分中介绍,并且具有以下示例,与C90的例子不同,它不需要特殊的算术来计算数组的大小:

 示例在声明之后:

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

结构struct s有一个灵活的数组成员d。使用这个
的典型方法是:

int m = / *一些值* /;
struct s * p = malloc(sizeof(struct s)+ sizeof(double [m]));

,假设对malloc的调用成功了,p
所指向的对象在大多数情况下的行为就像p被声明为:

struct {int n;双d [m]; } * p;

(在某些情况下,这种等价性被打破;特别是,成员d的
抵消额可能不一样)。


Zero-length arrays are allowed in GNU C. and can be initialized thus

struct line {
       int length;
       char contents[0];
     };

     struct line *thisline = (struct line *)
       malloc (sizeof (struct line) + this_length);
     thisline->length = this_length;

Note: I am referring to this page here: http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html (provides a basic intro to variable length structures in C)

It goes on to say: "In ISO C90, you would have to give contents a length of 1, which means either you waste space or complicate the argument to malloc."

What does that mean? Could someone provide an example of how you can initialize variable length structs in C90 to aid understanding?

解决方案

If you really have to use c90 then the C FAQ has this covered in Question 2.6 :

struct name {
int namelen;
char namestr[1];
};

struct name *ret =
    malloc(sizeof(struct name)-1 + strlen(newname)+1);
            /* -1 for initial [1]; +1 for \0 */

Although the FAQ does say:

It's not clear if it's legal or portable, but it is rather popular. An implementation of the technique might look something like this.

Although the gcc document basically says they support it, in C99 as the FAQ says they added flexible array members, which I cover in this answer which is covered in section 6.7.2.1 Structure and union specifiers and has the following example, which unlike the C90 example does not require special math to account for the size of the array:

EXAMPLE After the declaration:

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

the structure struct s has a flexible array member d. A typical way to use this
is:

    int m = /* some value */;
    struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));

and assuming that the call to malloc succeeds, the object pointed to by p
behaves, for most purposes, as if p had been declared as:

     struct { int n; double d[m]; } *p;

(there are circumstances in which this equivalence is broken; in particular, the
 offsets of member d might not be the same).

这篇关于C90中的可变长度结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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