Can different optimization levels lead to functionally different code?

前端 未结 13 1938
[愿得一人]
[愿得一人] 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:11

    a.c:

    char *f1(void) { return "hello"; }
    

    b.c:

    #include 
    
    char *f1(void);
    
    int main()
    {
        if (f1() == "hello") printf("yes\n");
            else printf("no\n");
    }
    

    Output depends on whether merge string constants optimization is enabled or disabled:

    $ gcc a.c b.c -o a -fno-merge-constants; ./a
    no
    $ gcc a.c b.c -o a -fmerge-constants; ./a
    yes

提交回复
热议问题