Okay, I can't find a thread that I posted to just yesterday and recieved an email showing a nice reply by Narue, but I deleted it. I can't find that thread! Was it deleted? If it was, Narue, can you explain the reason atoi() is bad and the better way to do it again? If I'm just going crazy and it still exists, please post a link.

Sorry for posting such a stupid thread, but I would like the information.

>>f it was, Narue, can you explain the reason atoi() is bad

There is very little "bad" about atoi() -- you were using it incorrectly. It does have one problem in that it does not detect integer overflow. If you are going to use a function and don't really understand how it works, then RTFM! You don't drive a car by guesswork, so what makes you think you can guess at the parameters to functions.

If you are going to use a function and don't really understand how it works, then RTFM!

Okay, you can STFU!

You don't drive a car by guesswork, so what makes you think you can guess at the parameters to functions.

Well, I thought since I just magically knew the atoi() function existed, then I would magically know the parameter.

http://www.daniweb.com/techtalkforums/thread39656.html

Thanks Narue.

Member Avatar for iamthwee

Okay, you can STFU!

Well, I thought since I just magically knew the atoi() function existed, then I would magically know the parameter.

Thanks Narue.

Why not just stringstream server? Since you come from a java background it should be more familiar, since it directly converts a string to an integer or float? For more information RTFW

Okay, you can STFU!

LoL

For more information RTFW

That was pretty funny, actually.


I thought about it, but it looks like more work than has to be done.

Member Avatar for iamthwee

That was pretty funny, actually.


I thought about it, but it looks like more work than has to be done.

Possibly, if all your work is catered for char arrays. Anyway, it's just a suggeston for future reference. Stringstream is pretty simple as well.

>There is very little "bad" about atoi()
On the contrary, atoi sucks ass. Two points come directly to mind about why atoi is bad, and they're hardly negligable:

1) Undefined behavior if the converted value doesn't fit in an integer.
2) Impossible to properly check for errors.

For point 1, the only way to avoid it is to validate the string before giving it to atoi, and that completely defeats the point of using atoi in favor of strtol, which is simplified code. For point 2, you're SOL. If you define this as not bad, then I would be wary of any code you write.

>It does have one problem in that it does not detect integer overflow.
Undefined behavior is far from this benign. Not only does atoi fail to detect integer overflow, it destroys any predictability your program will have from that point on. Any instance of undefined behavior causes the entire program to be undefined.

Then there's the common mistake of trying to call atoi on a single character, which is what server_crash was trying to do. That suggests to me that the definition of atoi is flawed because it encourages misinterpretation (much like getchar), so that's three strikes against atoi and it's out of the game for any self-respecting programmer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.