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

后端 未结 6 501
栀梦
栀梦 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:40

    There are some ambigous cases concerning the order of execution in parameter lists or e.g. additions.

    #include 
    
    using namespace std;
    
    int a() {
        cout << "Eval a" << endl;
        return 1;
    }
    
    int b() { 
        cout << "Eval b" << endl;
        return 2;
    }
    
    int plus(int x, int y) {
        return x + y;
    }
    
    int main() {
    
        int x = a() + b();
        int res = plus(a(), b());
    
        return 0;
    }
    

    Is a() or b() executed first? ;-)

提交回复
热议问题