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

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

问题描述

在阅读了一些关于flexible array member的帖子后,我仍然没有完全理解为什么我们需要这样的功能.

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

可能的重复:
C 中的灵活数组成员 - 不好?
这是 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).而且我也知道灵活的数组成员只能作为结构体的最后一个元素出现,所以原来的实现在data的位置上可以更加灵活.

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 有一个整数和一个指向 char 的指针.h2 有一个整数和一个 inline 字符数组(在运行时确定的元素数量,可能没有).

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 可能有意义的一种情况是,如果您正在与需要 {length,data} 对形式的消息的东西进行通信.你通过请求sizeof(h2)+你想要的有效载荷字符数来分配一个h2的实例,填满它,然后你就可以在一个write(当然要注意字节序等).如果您使用了 h1,则需要两次 write 调用(除非您想发送数据的内存地址,这通常没有任何意义).

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天全站免登陆