conversions to int/double/etc...
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.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>>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.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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 RTFWOkay, you can STFU!
LoL
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
For more information RTFW
That was pretty funny, actually.
I thought about it, but it looks like more work than has to be done.
server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
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.
iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
>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.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401