什么是灵活的数组成员的真正好处? [英] What are the real benefits of flexible array member?

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

问题描述

阅读相关的一些岗位后的灵活的数组成员后,我仍然无法完全理解为什么我们需要这样的功能。

After reading some posts related to flexible array member, I am still not fully understand why we need such a feature.

可能重复:结果
  灵活数组成员用C - 坏结果
  <一href=\"http://stackoverflow.com/questions/13622797/is-this-a-flexible-array-struct-members-in-c-as-well\">Is这是一个灵活的数组结构成员用C呢?

(怪我,如果我不从上面可能重复的问题,解决我的问题)

(Blame me if I didn't solve my problem from the possible duplicate questions above)

是什么真正下列两种实现方式的区别:

What is the real difference between the following two implementations:

struct h1 {
    size_t len;
    unsigned char *data;
};

struct h2 {
    size_t len;
    unsigned char data[];
};

我知道H2的大小犹如灵活的数组成员(数据)的省略,也就是的sizeof(H2)==的sizeof(为size_t)。而且我也知道,灵活的数组成员只能出现结构的最后一个元素,所以原来的实现可以在数据的位置更加灵活。

I know the size of h2 is as if the flexible array member (data) were omitted, that is, sizeof(h2) == sizeof(size_t). And I also know that the flexible array member can only appear as the last element of a structure, so the original implementation can be more flexible in the position of data.

我的真正的问题是,为什么C99添加此功能?很简单,因为的sizeof(H2)不包含数据的实际尺寸是多少?我相信,我必须错过了一些比较重要的点此功能。请指出来给我。

My real problem is that why C99 add this feature? Simply because sizeof(h2) doesn't contain the real size of data? I am sure that I must miss some more important points for this feature. Please point it out for me.

推荐答案

在您的文章中两个结构不具有相同的结构都没有。 H1 有一个整数和一个字符指针。 H2 有一个整数,字符数组的在线(在运行时,可能没有一个确定的元素的个数)。

The two structs in your post don't have the same structure at all. h1 has a integer and a pointer to char. h2 has an integer, and an array of characters inline (number of elements determined at runtime, possibly none).

换个说法就是,在 H2 字符数据是结构内部。在 H1 它必须之外的某处是。

Said differently, in h2 the character data is inside the struct. In h1 it has to be somewhere outside.

这使得有很大的区别。举例来说,如果你使用 H1 你需要采取分配/释放有效载荷(除了结构本身)的照顾。随着 H2 中,只有一个分配/释放是必要的,一切都打包在一起。

This makes a lot of difference. For instance, if you use h1 you need to take care of allocating/freeing the payload (in addition to the struct itself). With h2, only one allocation/free is necessary, everything is packaged together.

一情况下使用 H2 可能是有意义的是,如果你的东西,在形式预计信息通信 {长度,数据} 对。您可以通过请求分配 H2 的实例的sizeof(H2)+你要多少有效载荷字符,填满它,然后你就可以在一个单一的写入传输整个事情(注意有关字节序和等,当然)。如果你已经使用 H1 ,你需要两个电话(除非你想发送的内存地址数据,通常没有任何意义)。

One case where using h2 might make sense is if you're communicating with something that expects messages in the form of {length,data} pairs. You allocate an instance of h2 by requesting sizeof(h2)+how many payload chars you want, fill it up, and then you can transfer the whole thing in a single write (taking care about endianess and such of course). If you had used h1, you'd need two write calls (unless you want to send the memory address of the data, which usually doesn't make any sense).

所以这个功能存在,因为它很方便。并在之前使用的各种(有时是不可移植)的技巧来模拟这个功能。将其添加到标准是有道理的。

So this feature exists because it's handy. And various (sometimes non-portable) tricks where used before that to simulate this feature. Adding it to the standard makes sense.

这篇关于什么是灵活的数组成员的真正好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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