You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

向函数传递同一联合体成员的两个指针是否违反严格别名规则?

严格别名规则学习内容

说明:当前为严格别名规则学习相关内容,请知悉。

代码示例(t935.c)

#include <stdio.h>

int f(int* pi, double* pd)
{
    *pi = 13;
    *pd = 7E-323;
    return *pi;
}

int main(void)
{
    union { int i; double d; } u;
    printf("%d\n", f(&u.i, &u.d));
    return 0;
}

编译运行命令及结果

$ gcc t935.c -Wall -Wextra -std=c11 -pedantic  && ./a.exe
14

$ gcc t935.c -Wall -Wextra -std=c11 -pedantic -O2 && ./a.exe
13

$ gcc t935.c -Wall -Wextra -std=c11 -pedantic -O2 -fno-strict-aliasing && ./a.exe
14

核心问题

向函数传递两个指向同一联合体成员的指针是否违反严格别名规则?

2021年9月1日更新

如果联合体类型定义在全局作用域会出现什么情况?

针对“联合体u定义在全局作用域(实际为文件作用域)”的场景,gcc和clang的运行结果与上述gcc的测试结果完全一致。


内容的提问来源于stack exchange,提问作者pmor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 00:15:03