Is there anything you could supply to the atoi function that would produce an error (that may or may not crash the program)?
EDIT: An error is defined as anything that would produce a compilation error, or something that would cause the program to terminate during execution.
anything that would produce a compilation error
If the argument you supply to atoi()
is of an incompatible type, you'll get a compile-time error.
something that would cause the program to terminate during execution
If you supply atoi()
with an invalid const char*
pointer, the behaviour of your code will be undefined. While nothing is guaranteed, if the pointer is NULL
or points to unreadable memory the program will likely terminate (this depends on the OS and the hardware architecture).
I suspect the question is more along the lines of 'do I have to sanitize the input from a source I dont trust before passing it to atoi?'
The behavior of a correctly written atoi function is specified for most cases. It will convert characters to numbers until it hits something non numeric and then stop. However atoi is considered vulnerable to overflows
You should use strtol instead; its spec is tighter
Of course the implementation in your c runtime could be broken - but then there is not a lot you can do about that
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.