开发者

Expression in C++

int main() {
  int a = 10;
  int b = a * a++;
 开发者_JAVA百科 printf("%i %i", a, b);
  return 0;
}

Is the output of the above code undefined behavior?


No in

int b = a * a++;

the behavior is undefined, so the result can be anything - that's not what "implementation dependent" means.

You might wonder why it's UB here since a is modified only once. The reason is there's also a requirement in 5/4 paragraph of the Standard that the prior value shall be accessed only to determine the value to be stored. a shall only be read to determine the new value of a, but here a is read twice - once to compute the first multiplier and once again to compute the result of a++ that has a side-effect of writing a new value into a. So even though a is modified once here it is undefined behavior.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜