banjobyster's blog

By banjobyster, history, 3 years ago, In English

UPD — seems the sieve function in my code caused some undefined behavior since it was getting out of bound, so I have got my answer. Thank you all who came forward to help. (I didn't even needed that function, it was left from the previous code, seems it was my unlucky day, atleast, got to learn something ;-;)

Why did I get runtime error for this code of mine in the competition?

link: https://codeforces.me/contest/1562/submission/127119400

While the same got accepted just after the competition

link: https://codeforces.me/contest/1562/submission/127129361

(exact same code)

PS- ;-; I am trying so hard to get better but these types of problem are so demotivating

MikeMirzayanov(edit-removed tag) please do help if possible ( I really don't want to be irritating ranting about my problem everywhere, but you see, this feels a big achievement to me, watching myself getting better everyday, and yes, ratings matter for me, rankings matter for me, it is kind of an adrenaline boost to me, so please help if possible ;-;)

  • Vote: I like it
  • +10
  • Vote: I do not like it

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by banjobyster (previous revision, new revision, compare).

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I think the issue is legit, the exact same code got an AC that got an RTE previously in the contest. It'd be great if someone from headquarters could review this issue once.

»
3 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

Your sieve function contains undefined behaviour (your index goes out of bounds). It seems to me that you should change the line vi a(n,0); to vi a(n+1,0);

  • »
    »
    3 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thank you for your insight.

    Will update that part, but atleast, in this code, this shouldn't have mattered, reason being my solution got accepted just after the contest, I believe so.

    • »
      »
      »
      3 years ago, # ^ |
      Rev. 2   Vote: I like it +26 Vote: I do not like it

      When you have undefined behaviour in your code, all bets are off. You just got unlucky with bad RNG during the contest. I just submitted the same code and it passed 6 times and failed the 7th time. Btw. when you get a runtime error, it's probably almost always an out of bounds error, so look for that in the future.

      What's funny is that you could have just submitted the same code and probably passed the tests and this meme would actually come true hahaha

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Ok, seems I have to accept it and move on, maybe try getting better ;-;

      • »
        »
        »
        »
        3 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Shall I update the post? Or let it be?

  • »
    »
    3 years ago, # ^ |
      Vote: I like it +6 Vote: I do not like it

    And I mistakenly kept the sieve function, that was from the previous question ;-;

    But still that passed but this didn't, and for test case number 35 gave runtime error

»
3 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by banjobyster (previous revision, new revision, compare).

»
3 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

If you compile with the -D _GLIBCXX_DEBUG and -D _GLIBCXX_DEBUG_PEDANTIC flags, your program will always crash when you run it locally and it gets an out of bounds error. The program will run slower, but it's really worth avoiding the chance of terrible undefined behavior

g++ A.cpp -Wall -Wextra -D _GLIBCXX_DEBUG -D _GLIBCXX_DEBUG_PEDANTIC