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

Автор NeverMa573r, история, 15 месяцев назад, По-английски

Anybody know why this happens? I'm completely thrown off on how to debug this, since the bug disappears when I do cout. (this is only on my local machine, but I will provide the code)

My submission : https://codeforces.me/contest/1851/submission/221618285

Please provide some tips, because google is not.

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

»
15 месяцев назад, # |
  Проголосовать: нравится +21 Проголосовать: не нравится

This is a Heisenbug. Nine out of ten times it's gonna be caused by accessing out-of-bounds arrays. It's generally caused by undefined behavior in your code.

Opening the link you provided, the Codeforces diagnostic tells you exactly on which like you went out of bounds. Why was this not enough for you to debug the problem?

  • »
    »
    15 месяцев назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    I suppose his question was why the error disappears when he adds a cout

    • »
      »
      »
      15 месяцев назад, # ^ |
        Проголосовать: нравится +5 Проголосовать: не нравится

      I suppose I was responding mostly to "I'm completely thrown off on how to debug this", presuming "this" refers to the particular instance. You do have a point, perhaps it was a general question.

      Unfortunately the answer is that a Heisenbug occurs because of some undefined behavior in your code whose results change depending on unrelated code (e.g. cout). In theory, any undefined behavior is allowed to result in this, but in practice many things which are undefined behavior do not have crazy effects in practice. One thing that is very often a cause of Heisenbugs is accessing uninitialized memory, because this is extremely flaky and the behavior can change even if you change a completely different part of your code.

      As to how to debug it in practice, you should try to verify the bounds of all accesses. If you have some memory diagnostics/sanitizer tool or compiler options, you should try to run with those. You could also try to use debug printing as regular, but you have to treat it as if any seemingly innocent change you make results in a coin flip of whether the code crashes or not. I.e, you may have trouble printing the things you want, without changing behavior.