3 ms·
well, one of the problems could have been detected with "grep atoi source_code_here". any code that uses atoi is almost certainly bad (no error detection), and
by zabzonk 4y ago
well, one of the problems could have been detected with "grep atoi source_code_here". any code that uses atoi is almost certainly bad (no error detection), and C++ provides several alternatives.
- jeffbee 4y agoRight? Why do people love these C library functions? I think the biggest problem in the C++ community is that some people know a little C. The memcpy of the vector’s data sounds like it also might be non idiomatic, but I don’t see the source code so I am not sure.
- gpderetta 4y agoIdeally the compiler would suggest to replace atoi with the C++ equivalent. But not even clang-tidy does with the set of warnings I have enabled.
- pclmulqdq 4y agoThat's not the philosophy of the C++ compiler writers. There are linters that suggest it, but compilers don't do this sort of warning.
- zabzonk 4y agoi don't even understand why atoi gets used in pure C code - there are much better standard C alternatives.
- eklitzke 4y agoIn this case the code was using the .data() on a std::vector<char>, so it wouldn't have even been possible to use something like std::stoi (or something nicer like absl::SimpleAtoi) since the C++ routines need a std::string (or absl will accept std::string_view), not a C style string. Which in itself shows you that the code is problematic, because it's been written in a way where you can't even use idiomatic C++ library functions to do basic tasks. For the memcpy() thing the idiomatic way would have been to use std::copy() with the vectors begin/end iterators, and std::copy will do the right thing in this situation with a zero sized vector. The compiler will generate the same code with std::copy() as memcpy() anyway if the vector contains a POD type (as it did in this example), so there's not even a performance argument to be made for memcpy().