-1

This sounds wired but actually this is Real. Basically I was using well known Geeks for Geeks online compiler to submit c++ codes. Unfortunately my submission got failed as compiler showing that my code is not working properly for some test cases then I recheck my code and didn't found any issue so, I submitted that same code as is without changing a single bit (unconsciously) and I am shocked by seeing that my code runs all the test cases properly in that time. Now I'm willing to know how it is possible? Is it a compiler problem or anything else?

I already checked line wise line both codes from submission history and found out that both are absolutely same but one is accepted and another one is rejected. As per my knowledge, Online compilers runs same & predefined test cases over and again for a particular problem when we hit submit button

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
54Y4N
  • 19
  • 6
  • 4
    C++ programs can have undefined behavior and unspecified behavior. The former means that the output can be potentially _anything_ and the latter means that there are multiple allowed outcomes that may be produced. There is no reason that the program with the same input must always produce the same output. Also, we don't know of course what the site is doing with your submission. Is there any reason to believe that they always compile exactly the same, test the exact same testcases in the exact same environment, etc.? All in all, we can't know, but it is not surprising. – user17732522 Apr 15 '23 at 13:20
  • There are no compiler errors in this question, tag removed. – n. m. could be an AI Apr 15 '23 at 15:17

1 Answers1

0

Your submitted code probably has unspecified or undefined behavior.

When your code invokes such behavior, there are many possible factors which may cause your code to behave differently from a previous time you ran the same code.

For example, the addresses may change every time you run the program, due to Address Space Layout Randomization (ASLR). As a consequence, accessing an array out of bounds by a certain amount may trigger a page fault in some random memory layouts, but not in others.

Another possiblility is that since the first time you submitted your code, the third-party site that you are using may have updated their compiler to a new version, or that site may have changed the settings with which your code gets compiled. Both possibilities may cause the compiler to emit slightly different instructions, which changes the behavior of your code.

Or maybe your program's behavior did not change at all. Maybe the third-party site modified their test program in some way, for example by changing the input for testing your program.

If you want to ensure that your code has only one possible behavior, then you must write your code accordingly, by not doing anything which invokes unspecified or undefined behavior.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39