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

今天学习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)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值