结构体大小为0的数组 [英] Array of size 0 at the end of struct

查看:327
本文介绍了结构体大小为0的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



 

我的系统编程课程教授今天告诉我们要定义一个结构长度为零的数组。 code> struct array {
size_t size;
int data [0];
};

typedef结构数组数组;

这是一个有用的结构来定义或初始化一个变量的数组,即如下所示:

  array * array_new(size_t size){
array * a = malloc(sizeof(array)+ size * sizeof (INT));

if(a){
a-> size = size;
}

return a;

$ / code>

也就是说,使用 malloc(),我们还为大小为零的数组分配内存。这对我来说是全新的,看起来很奇怪,因为根据我的理解,结构中的元素并不一定位于连续的位置。



为什么代码位于 array_new 将内存分配给 data [0] ?为什么要访问它是合法的,比如说:

  array * a = array_new(3); 
a-> data [1] = 12;



从他告诉我们,看起来在struct的最后一个元素后面确定了一个数组定义为长度为零的数组,但这看起来很奇怪,因为根据我的理解,结构也可以有填充。



我也看到过,这只是gcc的一个特性,并没有被任何标准定义。这是真的吗?

解决方案

目前,存在一个标准特性,如C11第6.7.2.1章所述, em>灵活的数组成员。



引用标准,


作为一种特殊情况,具有多个命名成员的结构的最后一个元素可能
具有不完整的数组类型;这被称为灵活的数组成员。在大多数情况下,可变数组成员
被忽略。特别是,该结构的大小就像是
灵活数组成员被省略,除了它可能具有比
更多的尾部填充,这是遗漏意味着的。 [b]

语法应该是

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

最后一个元素是不完整的类型,( 没有数组维度,甚至不是因此,你的代码应该看起来像

<$ p $

p> 结构数组{
size_t size;
int data [];
};

符合标准。

现在来看你的例子,一个0大小的数组,这是一个传统的方法(struct hack)。在 C99 之前, GCC支持将此作为扩展名 a>来模拟灵活的数组成员功能。


My professor of a systems programming course I'm taking told us today to define a struct with a zero-length array at the end:

struct array{
    size_t size;
    int data[0];
};

typedef struct array array;

This is a useful struct to define or initialize an array with a variable, i.e., something as follows:

array *array_new(size_t size){
    array* a = malloc(sizeof(array) + size * sizeof(int));

    if(a){
        a->size = size;
    }

    return a;
}

That is, using malloc(), we also allocate memory for the array of size zero. This is completely new for me, and it's seems odd, because, from my understanding, structs do not have their elements necessarily in continuous locations.

Why does the code in array_new allocate memory to data[0]? Why would it be legal to access then, say

array * a = array_new(3);
a->data[1] = 12;

?

From what he told us, it seems that an array defined as length zero at the end of a struct is ensured to come immediately after the last element of the struct, but this seems strange, because, again, from my understanding, structs could have padding.

I've also seen around that this is just a feature of gcc and not defined by any standard. Is this true?

解决方案

Currently, there exists a standard feature, as mentioned in C11, chapter §6.7.2.1, called flexible array member.

Quoting the standard,

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. [...]

The syntax should be

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

where the last element is incomplete type, (no array dimensions, not even 0).

So, your code should better look like

struct array{
    size_t size;
    int data[ ];
};

to be standard-conforming.

Now, coming to your example, of a 0-sized array, this was a legacy way ("struct hack") of achieving the same. Before C99, GCC supported this as an extension to emulate flexible array member functionality.

这篇关于结构体大小为0的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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