3 ms·
strtol does fail on overflow [1] -- setting errno to ERANGE and returning LONG_MIN or LONG_MAX. Since those are also valid return values and it doesn't update
by ksherlock 13y ago
strtol does fail on overflow [1] -- setting errno to ERANGE and returning LONG_MIN or LONG_MAX. Since those are also valid return values and it doesn't update errno on success, it's recommended to set errno to 0 first.
Or, you could c++11 std::stol [2], et alia, which are similar (libc++ is a wrapper around strtol) but throw an exception on overflow.
1. http://pubs.opengroup.org/onlinepubs/009696699/functions/strtol.html http://pubs.opengroup.org/onlinepubs/009696699/functions/str...
2. http://en.cppreference.com/w/cpp/string/basic_string/stol http://en.cppreference.com/w/cpp/string/basic_string/stol