[英]What's the effect of __attribute__ ((__packed__)) on nested structs?
What is the effect of __attribute__ ((__packed__))
on nested structs? __attribute__ ((__packed__))
对嵌套结构有什么影响? For example: 例如:
// C version
struct __attribute__ ((__packed__))
{
struct
{
char c;
int i;
} bar;
char c;
int i;
} foo;
// C++ version
struct __attribute__ ((__packed__)) Foo
{
struct Bar
{
char c;
int i;
} bar;
char c;
int i;
} foo;
I know foo
will be tightly packed, but what about bar
? 我知道
foo
会紧紧包装,但是bar
呢? Will it too be tightly packed? 它会紧紧包装吗? Does
__attribute__ ((__packed__))
make the nested struct
also packed? __attribute__ ((__packed__))
是否使嵌套struct
也被打包?
No, bar
will not be tightly packed. 不,
bar
不会紧紧包装。 It must be explicitly marked as __attribute__ ((__packed__))
if it is to be packed. 如果要打包,它必须明确标记为
__attribute__ ((__packed__))
。 Consider the following example: 请考虑以下示例:
#include <stdio.h>
struct
{
struct
{
char c;
int i;
} bar;
char c;
int i;
} foo1;
struct __attribute__ ((__packed__))
{
struct
{
char c;
int i;
} bar;
char c;
int i;
} foo2;
struct
{
struct __attribute__ ((__packed__))
{
char c;
int i;
} bar;
char c;
int i;
} foo3;
struct __attribute__ ((__packed__))
{
struct __attribute__ ((__packed__))
{
char c;
int i;
} bar;
char c;
int i;
} foo4;
int main()
{
printf("sizeof(foo1): %d\n", (int)sizeof(foo1));
printf("sizeof(foo2): %d\n", (int)sizeof(foo2));
printf("sizeof(foo3): %d\n", (int)sizeof(foo3));
printf("sizeof(foo4): %d\n", (int)sizeof(foo4));
return 0;
}
The output of this program (compiling with gcc 4.2, 64-bits and clang 3.2, 64-bits) is: 该程序的输出(使用gcc 4.2,64位和clang 3.2,64位编译)是:
sizeof(foo1): 16
sizeof(foo2): 13
sizeof(foo3): 12
sizeof(foo4): 10
If a struct
and its nested struct
s are to all be tightly packed, __attribute__ ((__packed__))
must be explicitly declared for each struct
. 如果要将
struct
及其嵌套struct
全部紧密打包,则必须为每个struct
显式声明__attribute__ ((__packed__))
。 This makes sense, if you think of separating the nesting out so that bar
's type is declared outside of foo
, like so: 这是有道理的,如果你想将嵌套分开,以便在
foo
之外声明bar
的类型,如下所示:
// Note Bar is not packed
struct Bar
{
char c;
int i;
};
struct __attribute__ ((__packed__))
{
// Despite foo being packed, Bar is not, and thus bar will not be packed
struct Bar bar;
char c;
int i;
} foo;
In the above example, for bar
to be packed, Bar
must be declared as __attribute__ ((__packed__))
. 在上面的示例中,对于要打包的
bar
, Bar
必须声明为__attribute__ ((__packed__))
。 If you were to copy 'n' paste these structures in order to nest them like in the first code example, you'll see that the packing behavior is consistent. 如果您要复制'n'粘贴这些结构以便像第一个代码示例中那样嵌套它们,您将看到打包行为是一致的。
Corresponding C++ code (compiled with g++ 4.2 and clang++ 3.2, targeting 64-bits, which gives the exact same results as above): 相应的C ++代码(使用g ++ 4.2和clang ++ 3.2编译,目标是64位,它提供与上面完全相同的结果):
#include <iostream>
struct Foo1
{
struct Bar1
{
char c;
int i;
} bar;
char c;
int i;
} foo1;
struct __attribute__ ((__packed__)) Foo2
{
struct Bar2
{
char c;
int i;
} bar;
char c;
int i;
} foo2;
struct Foo3
{
struct __attribute__ ((__packed__)) Bar3
{
char c;
int i;
} bar;
char c;
int i;
} foo3;
struct __attribute__ ((__packed__)) Foo4
{
struct __attribute__ ((__packed__)) Bar4
{
char c;
int i;
} bar;
char c;
int i;
} foo4;
int main()
{
std::cout << "sizeof(foo1): " << (int)sizeof(foo1) << std::endl;
std::cout << "sizeof(foo2): " << (int)sizeof(foo2) << std::endl;
std::cout << "sizeof(foo3): " << (int)sizeof(foo3) << std::endl;
std::cout << "sizeof(foo4): " << (int)sizeof(foo4) << std::endl;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.