Блог пользователя JeevanJyot

Автор JeevanJyot, 3 года назад, По-английски

My friend the_hyp0cr1t3 and I came across this weird behaviour which we can't seem to explain.

The submissions 121761224 and 121761200 are identical, yet the former (submitted in C++17) gets accepted and the latter (submitted in C++14) gets WA on test 1.

After an hour of debugging, we managed to get it accepted in C++14 (121761314) with the following change in the code:

WA (C++14)
AC (C++14)

We would appreciate if someone could explain why something like a = b = c; and a = c; b = a; seem to produce different results.

  • Проголосовать: нравится
  • +27
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +55 Проголосовать: не нравится

If you have expression a = b then in C++17 b will be evaluated before a, but in C++14 the order is not guaranteed. In your example it leads to the fact that first st[node].lc is evaluated, which is a reference to something in vector st. Then create(lc) is evaluated, size of st increases and because of how vector works, all elements are moved and reference to st[node].lc becomes invalid, leading to UB.

If you replace vector with deque, it works, since references to elements in deque are always valid: https://codeforces.me/contest/813/submission/121763992