零元素数组需要什么? [英] What's the need of array with zero elements?

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

问题描述

在Linux内核代码中,我发现了以下我无法理解的东西.

In the Linux kernel code I found the following thing which I can not understand.

 struct bts_action {
         u16 type;
         u16 size;
         u8 data[0];
 } __attribute__ ((packed));

代码在这里:http://lxr.free-electrons.com/source/包括/linux/ti_wilink_st.h

零元素数据数组的需求和目的是什么?

What's the need and purpose of an array of data with zero elements?

推荐答案

这是一种拥有可变大小数据的方法,无需调用 malloc (kmalloc in这种情况)两次.你会像这样使用它:

This is a way to have variable sizes of data, without having to call malloc (kmalloc in this case) twice. You would use it like this:

struct bts_action *var = kmalloc(sizeof(*var) + extra, GFP_KERNEL);

这曾经不是标准的,被认为是一种黑客行为(如 Aniket 所说),但它在 C99 中标准化.现在它的标准格式是:

This used to be not standard and was considered a hack (as Aniket said), but it was standardized in C99. The standard format for it now is:

struct bts_action {
     u16 type;
     u16 size;
     u8 data[];
} __attribute__ ((packed)); /* Note: the __attribute__ is irrelevant here */

请注意,您没有提到 data 字段的任何大小.还要注意这个特殊变量只能出现在结构体的末尾.

Note that you don't mention any size for the data field. Note also that this special variable can only come at the end of the struct.

在 C99 中,这个问题在 6.7.2.1.16 中有解释(强调我的):

In C99, this matter is explained in 6.7.2.1.16 (emphasis mine):

作为一种特殊情况,具有多个命名成员的结构的最后一个元素可能有一个不完整的数组类型;这称为灵活数组成员.大多数情况下,灵活的数组成员被忽略.特别是,结构的大小就像灵活的数组成员被省略,除了它可能有更多的尾随填充比遗漏将意味着.但是,当一个 .(或 ->) 运算符有一个左操作数,即(指向)具有灵活数组成员和正确操作数名称的结构成员,它的行为就好像该成员被替换为最长的数组(具有相同的元素类型)不会使结构大于被访问的对象;这数组的偏移量应保持灵活数组成员的偏移量,即使这会有所不同从替换数组的那个.如果这个数组没有元素,它的行为就像它只有一个元素,但如果尝试访问该元素,则行为未定义元素或生成一个指针.

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed; the offset of the array shall remain that of the flexible array member, even if this would differ from that of the replacement array. If this array would have no elements, it behaves as if it had one element but the behavior is undefined if any attempt is made to access that element or to generate a pointer one past it.

或者换句话说,如果你有:

Or in other words, if you have:

struct something
{
    /* other variables */
    char data[];
}

struct something *var = malloc(sizeof(*var) + extra);

您可以使用 [0, extra) 中的索引访问 var->data.请注意,sizeof(struct something) 只会给出其他变量的大小,即给 data 的大小为 0.

You can access var->data with indices in [0, extra). Note that sizeof(struct something) will only give the size accounting for the other variables, i.e. gives data a size of 0.

注意到标准实际上是如何给出malloc这样一个结构的例子可能也很有趣(6.7.2.1.17):

It may be interesting also to note how the standard actually gives examples of mallocing such a construct (6.7.2.1.17):

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

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

同一位置的标准的另一个有趣注释是(强调我的):

Another interesting note by the standard in the same location is (emphasis mine):

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

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;

(在某些情况下,这种等价性会被破坏;特别是,成员 d 的偏移量可能不相同).

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

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

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