首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >__attribute__ ((__packed__))对嵌套结构有什么影响?

__attribute__ ((__packed__))对嵌套结构有什么影响?
EN

Stack Overflow用户
提问于 2013-06-04 03:43:45
回答 1查看 7.1K关注 0票数 6

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

代码语言:javascript
复制
// 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也打包吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-04 03:43:45

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

代码语言:javascript
复制
#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;
}

本程序(使用64位的gcc 4.2和64位的clang 3.2编译)的输出为:

代码语言:javascript
复制
sizeof(foo1): 16
sizeof(foo2): 13
sizeof(foo3): 12
sizeof(foo4): 10

如果要将struct及其嵌套的struct都紧密打包,则必须为每个struct显式声明__attribute__ ((__packed__))。如果您想要分离嵌套,以便在foo外部声明bar的类型,这是有意义的,如下所示:

代码语言:javascript
复制
// 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;

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

对应的C++代码(使用g++ 4.2和clang++ 3.2编译,目标是64位,结果与上面完全相同):

代码语言:javascript
复制
#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;
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16904613

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档