首页 > 解决方案 > 如何使用联合或可能使用枚举进行别名?

问题描述

我已经浏览了这篇非常好的文章

https://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule

但是我不了解使用 union 的别名,我对别名的了解是 with char/ unsigned char,根据这篇文章,这是一个编译器异常。所以,我过去是如何做混叠的char/ unsigned char

// lets say I have struct blah , having int member non_sense, cmmn_sense, intelligence...
struct blah *foo, *foo2;
unsigned char buffer [512];
foo = (struct blah *) (buffer+0);
//now accessing members
foo->non_sense = -1;
foo->non_snese = 0;
foo->intelligence =1;
// now we can use another struct after that ..
foo2 = (struct blah*) (buffer + sizeof(struct blah));

如果我在某些地方错了,请纠正我:)

问:那么我怎样才能对 union 做同样的事情呢?

注意我没有遇到多少联合,所以我也不知道它的正确用途。

我也搜索过别名enum,但不太了解,

问:如果可能的话,我们将如何使用枚举?

对不起我的英语,不是那么好,请公开建议纠正我在问题中的误解或术语以纠正......

标签: cenumsunionpointer-aliasing

解决方案


是的,几乎所有指针(包括您的示例)都是不安全的(除非它通过 访问char)。

下面是安全的(通过 char 指针访问):

struct blah x;
char *y = (char *)&x;

for(size_t i = 0; i < sizeof(x); i++)
   printf("[%zu] = %hhd\n", i, y[i];

正确的方法是memcpy缓冲到结构中或将 char 数组作为联合成员之一。

struct a
{
    int a;
    short b;
    char x[2];
    double z[3];
};


union b
{
    unsigned char buffer[512];
    struct a sa[512/32];
};

double foo(char *x)
{
    struct a c;
    memcpy(&c, x, sizeof(c));
    return c.z[0];
}

double bar(void)
{
    union b c;

    read_from_uart(&c, 512);

    return c.sa[5].z[1];
}

问:如果可能的话,我们将如何使用枚举?枚举是一个整数。与整数相同的规则。


推荐阅读