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?
I have no differences, this is the result of invocation of your test code compiled with
-std=c++20
:Execution of program compiled with
-std=c++17
gives the same result.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?
If you really interested in, just look into the implementation of the vector class template to see what actually happens.
Okay, I will do that, thanks!
Auto comment: topic has been updated by newbieToPupil (previous revision, new revision, compare).
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)