如何初始化包含可变数组成员的结构 [英] How to initialize a structure with flexible array member

查看:148
本文介绍了如何初始化包含可变数组成员的结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结构

typedef struct _person {
    int age;
    char sex;
    char name[];
}person;

我已经就如何创建一个实例并初始化具有灵活的阵列成员的结构不使用做了一些基本的互联网搜索(但不成功的)的malloc()

例如:对于像

struct a {
    int age; 
    int sex;
};

我们可以一个结构创建的一个实例,并初始化它像

We can create an instance of struct a and initialize it like

struct a p1 = {10, 'm'};

但对于它具有灵活的阵列结构(如 _person 如上所述)我们如何能够创建一个实例并初始化像我们如何做正常结构

But for structures with flexible array in it (like _person as mentioned above) how can we create an instance and initialize like how we do it for normal structures?

它甚至有可能?如果是这样,我们如何初始化和实际值中传递数组大小来初始化?

Is it even possible? If so, how do we pass the array size during the initialization and the actual value to be initialized?

(或)

这是真的,只有这样,才能建立具有灵活数组的结构是使用的malloc()在C99说明书中提到的 - 6.7.2.1结构和联合说明符 - ?!点#17

Is it true that the only way to create a structure with flexible array is using malloc() as mentioned in C99 specification - 6.7.2.1 Structure and union specifiers - point #17?!

推荐答案

没有,灵活的阵列必须始终手动分配。但是,您可以使用释放calloc 来初始化柔性部件和文字来初始化固定部分的化合物。我想换行,在分配在线这样的功能:

No, flexible arrays must always be allocated manually. But you may use calloc to initialize the flexible part and a compound literal to initialize the fixed part. I'd wrap that in an allocation inline function like this:

typedef struct person {
  unsigned age;
  char sex;
  size_t size;
  char name[];
} person;

inline
person* alloc_person(int a, char s, size_t n) {
  person * ret = calloc(sizeof(person) + n, 1);
  if (ret) memcpy(ret,
                  &(person const){ .age = a, .sex = s, .size = n},
                  sizeof(person));
  return ret;
}

观察到这留下支票如果分配成功给调用者。

Observe that this leaves the check if the allocation succeeded to the caller.

如果您不需要尺寸字段,因为我在这里包含它,宏甚至会足够了。只是,这将是不可能的检查释放calloc 操作前的的memcpy 的回报。在我编程,到目前为止,这将中止比较好听的所有系统。一般来说,我认为,<一个href=\"http://gustedt.word$p$pss.com/2011/11/05/chasing-a-phantom-checking-the-return-of-malloc/\">return 的malloc 是次要的,但意见在这个问题上变化很大。

If you don't need a size field as I included it here, a macro would even suffice. Only that it would be not possible to check the return of calloc before doing the memcpy. Under all systems that I programmed so far this will abort relatively nicely. Generally I think that return of malloc is of minor importance, but opinions vary largely on that subject.

这或许可以(在特殊情况)给予更多的机会,以优化到code在周围集成:

This could perhaps (in that special case) give more opportunities to the optimizer to integrate the code in the surroundings:

#define ALLOC_PERSON(A,  S,  N)                                 \
((person*)memcpy(calloc(sizeof(person) + (N), 1),               \
                 &(person const){ .age = (A), .sex = (S) },     \
                 sizeof(person)))

编辑:,这可能是比功能较好的情况是,当 A 取值是编译时常数。在这种情况下,复合文字,因为它是常量合格的,可以静态分配和初始化可以在编译时完成。另外,如果几个与分配相同的值将出现在code编译器将可以实现只有一个化合物的单拷贝文字。

The case that this could be better than the function is when A and S are compile time constants. In that case the compound literal, since it is const qualified, could be allocated statically and its initialization could be done at compile time. In addition, if several allocations with the same values would appear in the code the compiler would be allowed to realize only one single copy of that compound literal.

这篇关于如何初始化包含可变数组成员的结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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