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

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

问题描述

在Linux内核code我发现下面的事情,我无法理解。

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));

在code是在这里:<一href=\"http://lxr.free-electrons.com/source/include/linux/ti_wilink_st.h\">http://lxr.free-electrons.com/source/include/linux/ti_wilink_st.h

什么是数据的数组元素为零的需要,目的是什么?

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

推荐答案

这是一种方法,有数据的可变大小,而不必调用的malloc (<$ C在这种情况下,$ C>的kmalloc )的两倍。你会使用这样的:

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 */

请注意,你不任何规模大小为数据字段不在话下。另请注意,这个特殊变量只能来自于结构的结尾。

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):

作为一个特例,一个结构的一个以上的命名构件的最后一个元件可
  有一个不完整的数组类型; 这就是所谓的灵活数组成员。在大多数情况下,
   柔性阵列构件被忽略。具体地,结构的尺寸是因为如果
    除了灵活的阵列成员被省略,它可能有超过尾随填充
     遗漏意味着。然而,当一个。 (或 - >)运算符具有左操作数是
    (指针)具有灵活数组成员和正确的操作名称的结构
     部件,它的行为就好像该构件用最长阵列代替(具有相同的
    元素类型),不会使结构不是被访问的对象大;该
   阵列的偏移应保持柔性阵列构件的,即使这将不同
    从更换阵列。如果这个数组就没有的元素,它的行为就好像
   它有一个元素,但如果任何试图访问该行为是不确定
    元素或以产生的指针1过去它

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);

您可以访问 VAR-&GT;数据指数[0,另计)。需要注意的是的sizeof(结构的东西)只会给大小占其他变量,即给数据的大小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 ING这种结构的例子(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已被宣布为:

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的偏移量可能不一样的)。

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

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