Ad
  • Custom User Avatar

    Unfortunately, in C and C++ things are not that easy.

    Invalid memory access is defined in C++ as a cause of undefined behaior, and this can mean anything. In your case, your solution writes outside of the result buffer and modifies something which is later on used to calculate reference solution, or to print the message, or anything else. When undefined behavior happens, there are no guarantees anymore: anything can change, anything can break, even things apparently unrelated.

    If I were to guess, sprintf puts null terminator right behind your buffer, and probably g variable happens to be located there on the stack, messing up further computations. But it's just guessing, and it can be wrong or can change from run to run.

  • Custom User Avatar

    From all the things which are wrong with your solution, the wrongest ones are probably buffer overflow and missing includes.

  • Custom User Avatar
  • Custom User Avatar

    C++ tests seem to be fine.