What's the behavior of an uninitialized variable used as its own initializer?

后端 未结 3 1867
长情又很酷
长情又很酷 2020-11-27 22:35

I noticed just now that the following code can be compiled with clang/gcc/clang++/g++, using c99, c11, c++11 standards.



        
3条回答
  •  半阙折子戏
    2020-11-27 23:14

    This is a warning, it's not related to the standard.

    Warnings are heuristic with "optimistic" approach. The warning is issued only when the compiler is sure that it's going to be a problem. In cases like this you have better luck with clang or newest versions of gcc as stated in comments (see another related question of mine: why am I not getting an "used uninitialized" warning from gcc in this trivial example?).

    anyway, in the first case:

    int i = i;
    

    does nothing, since i==i already. It is possible that the assignment is completely optimized out as it's useless. With compilers which don't "see" self-initialization as a problem you can do this without a warning:

    int i = i;
    printf("%d\n",i);
    

    Whereas this triggers a warning all right:

    int i;
    printf("%d\n",i);
    

    Still, it's bad enough not to be warned about this, since from now on i is seen as initialized.

    In the second case:

    int i = i + 1;
    

    A computation between an uninitialized value and 1 must be performed. Undefined behaviour happens there.

提交回复
热议问题