避免填充与" unfull"结构 [英] Avoidable padding with "unfull" structs

查看:233
本文介绍了避免填充与" unfull"结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图给一个逻辑的反例来这个答案表明分选结构的成员根据其规模将减少填充,当我遇到了什么在我看来不合逻辑。

想象一下下面的结构:

 一个结构
{
    int32_t一个;
    int16_t B:
};

的sizeof 这个结构通常会被填充到8个字节,以确保 A 是例如在阵列对准的结构A

现在像这些结构:

 结构乙
{
    结构一A,B;
    int16_t C,D;
};结构ç
{
    结构A中的;
    int16_t℃;
    结构体A B;
    int16_t D组;
};

正如预期的那样结构乙有大小20,由于填充。不过,我本来期望结构ç来有自填充大小为16的可能的避免,但的 ideone 和gcc(有或没有优化)给出一个大小为24字节,明确每个填充成员后2个字节。

我的理由是,结构A 在现实中只有6个字节,并应在必要时,在的结构A <数组来填充,例如/ code>或其结构B的应用。然而,在结构ç 的填充结构A 是不必要的, C 本来是放在哪里的填充 A 本来是和 D 和<$ C $相同C> b

为什么不编译器通过将尽量减少填充 C ,其中的填充 A 是<? / p>


P.S。据我所知,的sizeof(结构A)必须返回8。否则像 memset的(array_of_A,0,N * sizeof的* array_of_A),因为 array_of_A 将包含填充,而 N * sizeof的* array_of_A 会忽略填充。

我能想到的,可能是一个问题的唯一的事情就是上面则优化的sizeof(结构C)的比的sizeof 其所有成员。但是,我想不出哪里这样的事情可能会成为一个问题(即不基于未定义行为的使用)的情况。


解决方案

的memcpy(安培; someC.a,&安培; someA,sizeof的(someC.a))会写在 someC.c

这就是我试图()的有得到在与我的 sizeof的评论
是不同的。对于的memcpy()工作的sizeof(someC.a)将不得不
是的sizeof从不同(someA)刚刚好像是问了很多
麻烦,很难找到的bug。

I was trying to give a "logical" counter-example to this answer indicating that sorting the members of a struct based on their size would minimize padding, when I encountered what seems to me as illogical.

Imagine the following struct:

struct A
{
    int32_t a;
    int16_t b;
};

sizeof this struct would normally be padded to 8 bytes to make sure a is aligned for example in an array of struct A.

Now image these other structs:

struct B
{
    struct A a, b;
    int16_t c, d;
};

struct C
{
    struct A a;
    int16_t c;
    struct A b;
    int16_t d;
};

As expected struct B has size 20 due to padding. However, I would have expected struct C to have size 16 since padding could be avoided, but ideone and gcc (with or without optimization) give a size of 24 bytes, clearly padding 2 bytes after each of the members.

My reasoning is that struct A in reality has only 6 bytes and should be padded when necessary, for example in an array of struct A or its usage in struct B. However, in struct C padding of struct A is unnecessary and c could have been placed where the padding of a could have been and the same with d and b.

Why doesn't the compiler minimize the padding by putting c where the padding of a would be?


P.S. I understand that sizeof(struct A) must return 8. Otherwise something like memset(array_of_A, 0, N * sizeof *array_of_A) won't work properly since array_of_A would contain padding while N * sizeof *array_of_A would ignore that padding.

The only thing I can think of that could be a problem is that with the optimization above then sizeof(struct C) would be smaller than the sizeof of all its members. However, I can't think of a case where such a thing could become a problem (i.e. a usage that is not based on undefined behavior).

解决方案

memcpy(&someC.a, &someA, sizeof(someC.a)) would write over someC.c.

That's what I was trying to get at with my comment about sizeof()'s having to be different. For that memcpy() to work, sizeof(someC.a) would have to be different from sizeof(someA) which just seems to be asking for a lot of trouble and hard to find bugs.

这篇关于避免填充与&QUOT; unfull&QUOT;结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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