当前位置:网站首页>【C语言】anonymous/unnamed struct&&union

【C语言】anonymous/unnamed struct&&union

2022-06-11 10:58:00 sidemap

今天学习muduo代码时,发现在类中如下的方式声明了成员变量:

 private:
  union
  {
    struct sockaddr_in addr_;
    struct sockaddr_in6 addr6_;
  };

在类的实现中,直接作为了成员变量使用了addr_ 和 addr6_。

后来经过调查,发现了anonymous/unnamed struct&&union,引用网上一段描述:
Anonymous Union and Structure in C. In C11 standard of Canonymous Unions and structures were added. Anonymous unions/structures are also known as unnamed unions/structures as they don't have names. Definition is just like that of a normal union just without a name or tag.
翻译:
C中的匿名联合体及结构体。在C11标准中,追加匿名联合体或结构。匿名联合体或结构体由于没有名字也就是无名联合体或结构体。定义就像正常的联合体,只是不带有name或tag。

以前都是声明了一个struct/union为某个类型,然后用该类型创建变量。

typedef union
{
    int a;
    float b;
} union_type;

union_type var;

以上anonymous的方式,在类的对象中可以直接使用 obj.addr_ 或者 obj.addr6_。

写了一个测试程序来说明使用方式,这样看起来更加直观。

#include <stdlib.h>
#include <stdio.h>

struct A1
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;
};

struct A2
{
    union   // 8
    {
        int i;
        long long l;
    };
};

struct A3
{
    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A12  // 4+8 => 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };
};

struct A13  // 4+ 8+8 => 8+ 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A23 // 8+ 8+8
{
    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct A  // 4 + 8 + 8+8 => 8 + 8 + 8+8
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };
};

struct B
{
    union   // 4
    {
        char c;
        short s;
        int i;
    } __type;

    union   // 8
    {
        int i;
        long long l;
    };

    struct  // 4+8 => 8+8
    {
        int st_i;
        long long st_ll;
    };

    struct __NamedStruct
    {
        int type_st_i;
        long long type_st_ll;
    };
};


int main()
{
    struct A a;
    a.__type.i = 4;
    printf("a.__type.i=%d\n", a.__type.i);

    printf("assign anonymous union.\n");
    a.i = 5;
    printf("a.__type.i=%d\n", a.__type.i);
    printf("a.i=%d\n", a.i);

    printf("assign anonymous structure.\n");
    a.st_i = 7;
    a.st_ll = 15;
    printf("a.__type.i=%d\n", a.__type.i);
    printf("a.i=%d\n", a.i);
    printf("a.st_i=%d\n", a.st_i);
    printf("a.st_ll=%lld\n", a.st_ll);

    printf("sizezof(struct A)=%lu\n", sizeof(struct A));        // 32
    printf("sizezof(struct B)=%lu\n", sizeof(struct B));        // 32

    printf("sizezof(struct A1)=%lu\n", sizeof(struct A1));      // 4
    printf("sizezof(struct A2)=%lu\n", sizeof(struct A2));      // 8
    printf("sizezof(struct A3)=%lu\n", sizeof(struct A3));      // 16
    printf("sizezof(struct A12)=%lu\n", sizeof(struct A12));    // 16
    printf("sizezof(struct A13)=%lu\n", sizeof(struct A13));    // 24
    printf("sizezof(struct A23)=%lu\n", sizeof(struct A23));    // 24

    return 0;
}

通过以上的赋值及其占用空间大小 ,可以发现对应的对象已经创建。

但是如果在内部声明了一个类型,没有创建该类型的对象,并不占用整个类型的空间(参考struct B与size A的sizeof)。

原网站

版权声明
本文为[sidemap]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sidemap/article/details/125219842