newbieToPupil's blog

By newbieToPupil, history, 5 months ago, In English

In the recent CFR 950 div3-D, I came across a Runtime Error Code: -1073741819 when run with C++17 (GCC 7-32), but AC with C++20 (GCC 13-64). After reading several blogs, it hinted me that I'm making an out of bound mistake using pointers, which in fact I was. I was able to fix my code. And got AC from C++17 as well.

Original Code: 264208915 (C++17)

Fixed Code: 264209191 (C++17)

And I made a few speculations:

C++17 (GCC 7-32) throws a Runtime Error if we are accessing pointers out of last-bound, but C++20 (GCC 13-64) just auto-adjusts it to the last-most element.

My proofs:

vector<int> v = {4,3,2,5};
v.erase(v.begin() + 10);

Gives Runtime Error in C++17 (GCC 7-32): 264205342

Doesn't give Runtime Error in C++20 (GCC 13-64): 264214654 in fact, it just removes the Last Element (5), from the vector (tested locally).

My question to the experienced is that are my speculations correct? If yes, then why does C++20 (GCC 13-64) has this type of different behavior?

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

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

I have no differences, this is the result of invocation of your test code compiled with -std=c++20:

$ ./a.out
double free or corruption (out)

Execution of program compiled with -std=c++17 gives the same result.

  • »
    »
    5 months ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    so both of them result in undefined behavior of double free corruption right? It's not necessary that only the last element will be accessed in case of accessing out of bound memory?

    • »
      »
      »
      5 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      If you really interested in, just look into the implementation of the vector class template to see what actually happens.

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

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

»
5 months ago, # |
  Vote: I like it 0 Vote: I do not like it

not same but something similar happened with me. I submitted a code (which was running on my local machine)

in C++17 which got me a runtime error. (263429563)

Later on submitting the same code in C++20, got accepted. (263429794)