# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3831 |
3 | Radewoosh | 3646 |
4 | jqdai0815 | 3620 |
4 | Benq | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | gamegame | 3386 |
10 | ksun48 | 3373 |
# | User | Contrib. |
---|---|---|
1 | cry | 164 |
1 | maomao90 | 164 |
3 | Um_nik | 163 |
4 | atcoder_official | 160 |
5 | -is-this-fft- | 158 |
6 | awoo | 157 |
7 | adamant | 156 |
8 | TheScrasse | 154 |
8 | nor | 154 |
10 | Dominater069 | 153 |
Name |
---|
You have undefined behaviour in your code
I know my mistake is I defined an array locally and did't initialize it but why C++14 accept my code. Is C++14 have something difference when used a locally array ?
The compiler is different. I doubt standard has something to do with it.
It's literally called "undefined behavior": I suppose there isn't really a way to explain how. Your best bet is to avoid writing codes with undefined behaviors.
c++14 is old and you know old is 'gold'.
my computer is old too but of course is not gold &_&
I compiled your code on my personal computer using GNU++20 with all warnings turned on. The following is the only warning which the compiler reported.
I replaced the variable length array declaration and initialization with the following declaration
And your updated code was accepted when submitted to the GNU++20 compiler at Codeforces.
132300063
What is the compilation code to get all warnings ?
I used the following Warning Options
Check the following link for more details.
G++ Warning Options
Can you guide me to setup these compilation flags in VS Code (minGW compiler) ??
Check the information provided in the following link.
Using GCC with MinGW
You need to update the "args" line in the tasks.json file to add these options to the arguments passed to the g++ compiler.
Is C++20 backwards compatible? Like I'll be able to compile any code intended for C++17 without any warnings/errors?
Also, what more does it have to offer? E.g.
for (auto [a,b] : c)
this kind of decomposition was only introduced in newer versions of C++ if I'm not mistaken.In general, backward compatibility is fundamental software engineering requirement in programming language development. The answer to your first question is: it should be possible to compile and run successfully "most" older c++ programs that were written for C++17. In few cases, some features in earlier versions are deprecated and not supported in the latest language version. The compiler documentation should provide sufficient information about such features.
RE: your second question
Check the following link for new language features in C++20.
C++20
This is well and good, but I asked here so that I get to know the features prominent from competitive programming perspective, instead of reading the entire documentation :D
Check the "Removed features and deprecation" section in the following Wikipedia page.
C++20
From competitive programming perspective, I do not think using C++20 would produce so many backward incompatibility issues when older programs intended for C++17 are resubmitted.
Nonetheless, I recommend compiling programs locally with all warning options enabled before submitting it to the automatic judge, so as to avoid situations like the one reported in this blog. Note that the automatic judge reports compilation errors only, and ignores all warnings.
did U submit code after change (AC code ) in C++20 ?
Yes, I checked the updated code after fixing the warning.
132300063
I usually do not ignore any compilation warning, and try to make sure that the submitted code is warning-free with respect to the available g++ compiler warning options.
Fine , This means that the problem is in C++17 ?
Not in the C++ language version 2017, but the issue is in the GNU++17 compiler when allocating local memory space for variable-length array which ISO C++ standard forbids.
Note that the C++ compiler allocates enough memory size in the program stack for local variables inside a code block. This space is allocated by decreasing the stack pointer of the process by the required amount, and is de-allocated after executing the code block by restoring the stack pointer to its previous value before entering the block, provided that this memory size is known at compile-time. When dealing with variable-length arrays, the compiler does not have information about the exact size of the array. Therefore, the compiler should have dealt with the variable-length array declaration which violated ISO C++ standard by implicitly calling dynamic memory management library functions.
The issue is not with the "variable-length array" at all. The author declared an array in line 50
char a[n + 2][m + 2]
but didn't initialize it. As a result, there were garbage values in the array which raised undefined behavior.Just initialize the array with the null character
'\0'
. You will get AC in every compiler.Well, this sounds logical. But, the GNU++20 submission which failed on test 16 had the two-dimensional array initialized.
I double checked the submission before replacing the three lines 49-51 with:
The same issue persists even with the variable-length array initialized.
132386103
132386200
Update:
I have just noticed the issue with the variable-length initialization which failed on test 16.
132386542
The double-loop in lines 50 and 51 should have started with
i = 0
andj = 0
to initialize the first row and the first column properly.It seems that the implicit dynamic memory allocation library function call in GNU++14 initialized the first row and the first column of the variable-length array implicitly to 0, while the same library function call in GNU++20 did not initialize the allocated memory.
Only the first row and the first column were uninitialized in the original code. The GNU++14 library function did a good job in initializing the allocated memory.
Thanks for the helpful note.
There us no difference between versions of the worst language in the world. Python us better and faster
Are you kidding? C++ is fun!