How do I make an infinite empty loop that won't be optimized away?

前端 未结 13 1431
后悔当初
后悔当初 2020-12-23 13:16

The C11 standard appears to imply that iteration statements with constant controlling expressions should not be optimized out. I\'m taking my advice from this answer, which

13条回答
  •  天命终不由人
    2020-12-23 13:20

    It seems that this is a bug in the Clang compiler. If there isn't any compulsion on the die() function to be a static function, do away with static and make it inline:

    #include 
    
    inline void die(void) {
        while(1)
            ;
    }
    
    int main(void) {
        printf("begin\n");
        die();
        printf("unreachable\n");
    }
    

    It's working as expected when compiled with the Clang compiler and is portable as well.

    Compiler Explorer (godbolt.org) - clang 9.0.0 -O3 -std=c11 -pedantic-errors

    main:                                   # @main
            push    rax
            mov     edi, offset .Lstr
            call    puts
    .LBB0_1:                                # =>This Inner Loop Header: Depth=1
            jmp     .LBB0_1
    .Lstr:
            .asciz  "begin"
    

提交回复
热议问题