Which issues have you encountered due to sequence points in C and C++?

后端 未结 6 491
栀梦
栀梦 2021-02-04 20:39

Below are two common issues resulting in undefined behavior due to the sequence point rules:

a[i] = i++; //has a read and write between sequence points
i = i++;          


        
6条回答
  •  盖世英雄少女心
    2021-02-04 21:29

    Here are two good expressions that work for most C compilers, yet are ambiguous due to sequence points:

    x ^= y ^= x ^= y; // in-place swap of two variables
    

    And also

    int i=0;
    printf("%d %d %d", ++i, ++i, ++i);  // usually prints out 3 2 1... but not for all compilers!
    

提交回复
热议问题