C pointers and -O3 optimized semantic anomalies

83 views Asked by At

Code

#include <stdint.h>
#include <stdio.h>

void func(uint64_t* pInst)
{
    *pInst = 0xffffffffff;
    *pInst = *(uint32_t*)&(*pInst);
    uint64_t Ret = *pInst;
    printf ("Ret=%lld\n", Ret);
}

int main()
{
    uint64_t a;
    a = 0;
    func(&a);
    return 0;
}

gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)

Build & Run:

gcc -O3 test.cpp and gcc -O2 test.cpp
./a.out 
Ret=0

gcc -O1 test.cpp and gcc -O0 test.cpp
./a.out 
Ret=4294967295

I expect the return value to be 4294967295. I try g++, it works like gcc. And I try clang, it works will.

0

There are 0 answers