Блог пользователя Lemur95

Автор Lemur95, история, 2 года назад, По-английски

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?

  • Проголосовать: нравится
  • +16
  • Проголосовать: не нравится

»
2 года назад, # |
  Проголосовать: нравится +8 Проголосовать: не нравится

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

»
2 года назад, # |
  Проголосовать: нравится +48 Проголосовать: не нравится
  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 года назад, # ^ |
      Проголосовать: нравится +20 Проголосовать: не нравится

    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 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

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

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