c - 如何使用联合或可能使用枚举进行别名?
问题描述
我已经浏览了这篇非常好的文章
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
,但不太了解,
问:如果可能的话,我们将如何使用枚举?
对不起我的英语,不是那么好,请公开建议纠正我在问题中的误解或术语以纠正......
解决方案
是的,几乎所有指针(包括您的示例)都是不安全的(除非它通过 访问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];
}
问:如果可能的话,我们将如何使用枚举?枚举是一个整数。与整数相同的规则。
推荐阅读
- ios - 无法找到最新的可用模拟器运行时 - 在 iOS 上构建 RN 应用程序时出现 Image.xcassets
- html - Rails 'grouped_options_for_select' 编辑不起作用
- html - 如何为多种屏幕尺寸创建响应式设计?
- python - 'utf-16-le' 编解码器在 PYTHON 中读取 EXCEL 时无法解码字节
- python - 这个矩阵乘法如何在 python 中工作?
- javascript - 为什么这项工作在 javascript 中起作用?- 简单的问题
- reactjs - props.history.push 无法正常工作并重定向到同一路线
- python - Pandas 获取每组未来 6 个月数据的列值总和
- selenium - 使用 Selenium 实现 ZAP 的 GitLab CI
- javascript - 使用 js 网络摄像头代码在 Colab 上的 Darknet yolov3 预测非常慢