如何使用灵活的数组成员初始化结构
我有以下结构
typedef struct _person {
int age;
char sex;
char name[];
}person;
我已经完成了一些基本的互联网搜索(但不成功),了解如何创建实例并使用灵活的数组成员初始化结构而不使用 malloc()
。
例如:对于像我们这样的普通结构,
struct a {
int age;
int sex;
};
我们可以创建一个struct a
实例并像这样初始化它,
struct a p1 = {10, 'm'};
但是对于其中包含灵活数组的结构(例如上面提到的_person
)如何我们创建一个实例并初始化,就像我们如何处理普通的结构一样?
有可能吗?如果是这样,我们如何在初始化期间传递数组大小和要初始化的实际值?
(或者)
是否真的,创建具有灵活数组的结构的唯一方法是使用 C99 规范中提到的 malloc() - 6.7.2.1 结构和联合说明符 - 第 #17 点< /代码>?!
I have the following structure
typedef struct _person {
int age;
char sex;
char name[];
}person;
I have done some basic internet search (but unsuccessful) on how to create an instance and initialize a structure with a flexible array member without using malloc()
.
For example: for normal structures like
struct a {
int age;
int sex;
};
We can create an instance of struct a
and initialize it like
struct a p1 = {10, 'm'};
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?
(or)
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
?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,灵活数组必须始终手动分配。但是您可以使用 calloc 来初始化灵活部分,并使用复合文字来初始化固定部分。我将其包装在一个分配内联函数中,如下所示:
观察这会将分配是否成功的检查留给调用者。
如果您不需要我在此处包含的
size
字段,则宏甚至就足够了。只是在执行memcpy
之前不可能检查calloc
的返回。在我到目前为止编写的所有系统下,这都会相对较好地中止。一般来说,我认为返回malloc< /code> 不太重要
,但对此主题的看法差异很大。
这也许(在这种特殊情况下)为优化器提供更多机会将代码集成到周围环境中:
编辑:这可能比函数更好的情况是当
A 和
S
是编译时常量。在这种情况下,复合文字由于是 const 限定的,因此可以静态分配,并且可以在编译时完成其初始化。此外,如果代码中出现多个具有相同值的分配,则编译器将只能实现该复合文字的一个副本。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 allocationinline
function like this:Observe that this leaves the check if the allocation succeeded to the caller.
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 ofcalloc
before doing thememcpy
. Under all systems that I programmed so far this will abort relatively nicely. Generally I think that return ofmalloc
is of minor importance, but opinions vary largely on that subject.This could perhaps (in that special case) give more opportunities to the optimizer to integrate the code in the surroundings:
Edit: The case that this could be better than the function is when
A
andS
are compile time constants. In that case the compound literal, since it isconst
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.您可以使用一些技巧。这取决于您的具体应用。
如果你想初始化单个变量,你可以定义一个正确大小的结构:
问题是你必须使用指针转换将变量解释为“person”(例如“*(person *)(&your_variable)但您可以使用包含联合来避免这种情况:
因此,your_var.p 的类型为“person”。
您还可以使用宏来定义初始化程序,这样您只能编写一次字符串:
另一个问题是这个技巧不适合创建“person”数组。数组的问题是所有元素必须具有相同的大小。在这种情况下,使用
const char *
而不是char[]
更安全。或者使用动态分配;)There are some tricks you can use. It depends on your particular application.
If you want to initialise a single variable, you can define a structure of the correct size:
The problem is that you have to use pointer casting to interpret the variable as "person"(e.g. "*(person *)(&your_variable)". But you can use a containing union to avoid this:
so, your_var.p is of type "person".
You may also use a macro to define your initializer, so you can write the string only once:
Another problem is that this trick is not suitable to create an array of "person". The problem with arrays is that all elements must have the same size. In this case it's safer to use a
const char *
instead of achar[]
. Or use the dynamic allocation ;)具有灵活数组成员的结构类型可以被视为省略了灵活数组成员,因此您可以像这样初始化结构。
然而,没有分配灵活数组的成员,并且任何访问灵活数组的成员或形成指向超出其末尾的指针的尝试都是无效的。创建具有灵活数组成员(该数组中实际包含元素)的结构实例的唯一方法是为其动态分配内存,例如使用 malloc。
A structure type with a flexible array member can be treated as if the flexible array member were omitted, so you can initialize the structure like this.
However, there are no members of the flexible array allocated and any attempt to access a member of the flexible array or form a pointer to one beyond its end is invalid. The only way to create an instance of a structure with a flexible array member which actually has elements in this array is to dynamically allocate memory for it, for example with
malloc
.