在此基礎上很好的博客文章,The Strict Aliasing Situation is Pretty Bad,我已經放置一段代碼在線爲您進行測試:C給出基於優化級別(如新)不同的輸出
http://cpp.sh/9kht(輸出之間的變化 - O0和-02)
#include <stdio.h>
long foo(int *x, long *y) {
*x = 0;
*y = 1;
return *x;
}
int main(void) {
long l;
printf("%ld\n", foo((int *)&l, &l));
}
是否有某種這裏未定義行爲?
當我們選擇-O2水平時,內部發生了什麼?
這實際上是違反類型別名規則的一個很好的例子。我會把它作爲我的規範副本。 – SergeyA