简体   繁体   English

__attribute __((__ package___))对嵌套结构的影响是什么?

[英]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__)) . 在上面的示例中,对于要打包的barBar必须声明为__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.

相关问题 portable __attribute __((__ package ___)) - portable __attribute__ ((__packed__)) __attribute__((__packed__)) 和 __attribute__((__packed__)) 有什么区别; 和#pragma pack(1) - what is the difference between __attribute__((__packed__)); and #pragma pack(1) Visual C ++相当于GCC的__attribute __((__ package ___)) - Visual C++ equivalent of GCC's __attribute__ ((__packed__)) 有关__attribute __((__ packed__))的c ++编译错误的其他方法是什么? - What are other methods for a c++ compile error about __attribute__((__packed__))? GCC 上的 #pragma pack(push, n)/#pragma pack(pop) 和 __attribute__((__packed__,aligned(n))) 之间有什么区别? - What are the differences between #pragma pack(push, n)/#pragma pack(pop) and __attribute__((__packed__, aligned(n) )) on GCC? __attribute __((packed))替代 - __attribute__((packed)) alternative __attribute __((packed))在只有1个元素的结构上 - __attribute__((packed)) on a struct with only 1 element __attribute __((packed))会影响程序的性能吗? - Can __attribute__((packed)) affect the performance of a program? 是什么阻止__attribute __((packed))进入ISO C / C ++? - What prevents __attribute__((packed)) from getting into ISO C/C++? 如果我将字节数组转换为 __attribute__((packed, aligned(2))) 结构会发生什么? - What will happen if I cast a byte array to an __attribute__((packed, aligned(2))) struct?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM