Reputation: 10417
As we know, strtoXX functions return max(or min) value if overflow occur, for example, strtoul("999999999999999999999999999999999", NULL, 0)
returns ULONG_MAX
. But strtoul("0xffffffff", NULL, 0)
returns also ULONG_MAX
(if sizeof(unsigned long) == 4).
Then, how can I check overflow?
Upvotes: 1
Views: 92
Reputation: 43662
http://www.cplusplus.com/reference/cstdlib/strtoul/
If the value read is out of the range of representable values by an unsigned long int, the function returns ULONG_MAX (defined in ), and errno is set to ERANGE.
You're dealing with unsigned integers, so it doesn't matter what sign your integer has: they're all treated as unsigned integers. And 0xFF FF FF FF (assuming 4 bytes as you're doing) is negative, but it's being treated as +4294967295 (not 2's complement). And that's also exactly the value of ULONG_MAX
in that case.
Upvotes: 4