Информация об изменениях

Сообщение Re[2]: А вот не надо верить глазам своим :) от 11.11.2017 15:05

Изменено 11.11.2017 15:10 rg45

Re[2]: А вот не надо верить глазам своим :)
Здравствуйте, MTD, Вы писали:

MTD>Изменение одной переменной более одного раза не пересекая точку следования (sequence points) — UB.


Следует заметитть что, начиная с 11-го стандарта. точки следования убрали, как таковые, и ввели вместо них понятия "sequenced before" и "unsequenced". Но на результат это не повлияло, в данном случае, действительно имеем UB (поскольку вычисление операндов оператора сложения "unsequenced"):

ISO/IEC/14882-2011
1.9/15

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. —end note ] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either anotherside effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. [ Example:

void f(int, int);
void g(int i, int* v) {
i = v[i++]; // the behavior is undefined
i = 7, i++, i++; // i becomes 9
i = i++ + 1; // the behavior is undefined
i = i + 1; // the value of i is incremented
f(i = -1, i = -1); // the behavior is undefined
}

Re[2]: А вот не надо верить глазам своим :)
Здравствуйте, MTD, Вы писали:

MTD>Изменение одной переменной более одного раза не пересекая точку следования (sequence points) — UB.


Следует заметитть что, начиная с 11-го стандарта. точки следования убрали, как таковые, и ввели вместо них понятия "sequenced before" и "unsequenced". Но на результат это не повлияло, в данном случае, действительно имеем UB (поскольку вычисление операндов оператора сложения "unsequenced"):

ISO/IEC/14882-2011
1.9/15

Except where noted, evaluations of operands of individual operators and of subexpressions of individual expressions are unsequenced. [ Note: In an expression that is evaluated more than once during the execution of a program, unsequenced and indeterminately sequenced evaluations of its subexpressions need not be performed consistently in different evaluations. —end note ] The value computations of the operands of an operator are sequenced before the value computation of the result of the operator. If a side effect on a scalar object is unsequenced relative to either anotherside effect on the same scalar object or a value computation using the value of the same scalar object, the behavior is undefined. [ Example:

void f(int, int);
void g(int i, int* v) {
i = v[i++]; // the behavior is undefined
i = 7, i++, i++; // i becomes 9
i = i++ + 1; // the behavior is undefined
i = i + 1; // the value of i is incremented
f(i = -1, i = -1); // the behavior is undefined
}