Can different optimization levels lead to functionally different code?

前端 未结 13 1901
[愿得一人]
[愿得一人] 2020-12-13 12:39

I am curious about the liberties that a compiler has when optimizing. Let\'s limit this question to GCC and C/C++ (any version, any flavour of standard):

Is it possi

13条回答
  •  一向
    一向 (楼主)
    2020-12-13 13:23

    Two different C programs:

    foo6.c

    void p2(void);
    
    int main() {
        p2();
        return 0;
    }
    

    bar6.c

    #include 
    
    char main;
    
    void p2() {
        printf("0x%x\n", main);
    }
    

    When both modules are compiled into one excecutable with optimization levels one and zero, they print out two different values. 0x48 for -O1 and 0x55 for -O0

    Screenshot of terminal

    Here is an example of it working in my environment

提交回复
热议问题