質問

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top