unspecified behavior, undefined behavior, - , :
Such an implementation can handle the post-increment as follows:
tmp_1=c;
tmp_2 = tmp_1 + 1;
c = tmp_2;
tmp_1;
The original expression (c++)*(c++)has two sequences:
lhs_1=c;
lhs_2 = lhs_1 + 1;
c = lhs_2;
lhs_1;
rhs_1=c;
rhs_2 = rhs_1 + 1;
c = rhs_2;
rhs_1;
The order may be:
lhs_1=c; // read 'c'
lhs_2 = lhs_1 + 1; // calculate the incremented value
c = lhs_2; // write to 'c'
rhs_1=c; // read 'c'
rhs_2 = rhs_1 + 1; // calculate the incremented value
c = rhs_2; // write to 'c'
lhs_1 * rhs_1 // (3 * 4) new value of 'c' is 5
Or:
lhs_1=c; // read 'c'
rhs_1=c; // read 'c'
lhs_2 = lhs_1 + 1; // calculate the incremented value
c = lhs_2; // write to 'c'
rhs_2 = rhs_1 + 1; // calculate the incremented value
c = rhs_2; // write to 'c'
lhs_1 * rhs_1 // (3 * 3) new value of 'c' is 4
Or:
rhs_1=c; // read 'c'
rhs_2 = rhs_1 + 1; // calculate the incremented value
c = rhs_2; // write to 'c'
lhs_1=c; // read 'c'
lhs_2 = lhs_1 + 1; // calculate the incremented value
c = lhs_2; // write to 'c'
lhs_1 * rhs_1 // (4 * 3) new value of 'c' is 5
.... etc.
unspecified behavioris that he can evaluate lhs or rhs first. undefined behaviorlies in the fact that we read and write in a csequence without intermediate points.
source
share