Lemur95's blog

By Lemur95, history, 2 years ago, In English

Hello CF community,

I just stumbled upon this weird (at least for me) behaviour. I was trying to solve 1605C - Доминантный характер, I set the compiler to C++ 20 and sent this code 135233345 but got C.E. for whatever reason. Then I thought of switching back to C++ 17 and this time I got accepted 135233454 (Note that it is the same code).

Can someone tell me why this is happening?

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

»
2 years ago, # |
  Vote: I like it +8 Vote: I do not like it

The Click link at the bottom of the failed compilation seems to have enough details about the reason of the compilation error.

»
2 years ago, # |
  Vote: I like it +48 Vote: I do not like it
  1. The "whatever reason" for compilation error is available at the bottom of the submission page. Generally, on error, compiler messages should be available on any modern testing system. Do make use of that!

  2. The error itself is that the >> operator no longer accepts a raw char pointer. Relevant information and reasoning can be found here, for example. In a nutshell: reading a string into a raw char pointer is unsafe; it's safer to read into an char array of known length, and stop before that length is exceeded, taking the null terminator into account. All in all, good to see some safety features in C++20, it's about time they appeared.

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

    I did check the compiler message and thought that it had something to do with the variable's name, which made no sense. Good to know that reading a raw char pointer is not safe, thanks!

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

Check the following blog about this compilation error in C++20.

Can't use std::cin with char* or char array in C++20