c++ - __attribute__ ((__packed__)) 对嵌套结构的影响是什么?

标签 c++ c gcc g++

__attribute__ ((__packed__)) 对嵌套结构有什么影响?例如:

// 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;

我知道 foo 会很紧凑,但是 bar 呢?会不会也塞得很紧? __attribute__ ((__packed__)) 是否使嵌套的 struct 也打包?

最佳答案

不,bar 不会被紧紧地包裹。如果要打包,则必须显式标记为 __attribute__ ((__packed__))。考虑以下示例:

#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;
}

这个程序的输出(用 gcc 4.2,64 位和 clang 3.2,64 位编译)是:

sizeof(foo1): 16
sizeof(foo2): 13
sizeof(foo3): 12
sizeof(foo4): 10

如果 struct 及其嵌套的 struct 都被紧密打包,则必须显式声明 __attribute__ ((__packed__))每个结构。这是有道理的,如果您考虑将嵌套分开,以便 bar 的类型在 foo 之外声明,如下所示:

// 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;

在上面的例子中,要打包barBar必须声明为__attribute__ ((__packed__))。如果您要复制“n”粘贴这些结构以便像第一个代码示例中那样嵌套它们,您将看到打包行为是一致的。


相应的 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;
}

关于c++ - __attribute__ ((__packed__)) 对嵌套结构的影响是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16904613/

相关文章:

c++ - 如何在自定义 std::ostream 类中使用 std::endl

c++ - boost::noncopyable_::noncopyable 模拟移动构造函数/赋值

c - 我如何在 while 循环之外访问数组

c - 为什么我不能在用 C 编写的 tcp 客户端/服务器中正确接收非纯文本文件?

c - 释放函数中分配的内存

c++ - 无法从 basic_stringstream<char8_t> 读取 char8_t

c++ - 推力按键即时排序还是不同的方法?

c++ - 如何使用 C/C++ 可视化字节

c - LD_LIBRARY_PATH 被 GCC 忽略

linux - 如何在 linux 的标准共享库(C)中添加自己的自定义函数?