944,111 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5005
  • C RSS
Feb 16th, 2006
0

conversions to int/double/etc...

Expand Post »
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.
Similar Threads
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Feb 16th, 2006
0

Re: conversions to int/double/etc...

Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Feb 16th, 2006
0

Re: conversions to int/double/etc...

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,961 posts
since Aug 2005
Feb 17th, 2006
0

Re: conversions to int/double/etc...

Quote originally posted by Ancient Dragon ...
If you are going to use a function and don't really understand how it works, then RTFM!
Okay, you can STFU!

Quote ...
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.

Thanks Narue.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Feb 17th, 2006
0

Re: conversions to int/double/etc...

Quote originally posted by server_crash ...
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

Quote originally posted by server_crash ...
Okay, you can STFU!
LoL
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 17th, 2006
0

Re: conversions to int/double/etc...

Quote originally posted by iamthwee ...
For more information RTFW
That was pretty funny, actually.


I thought about it, but it looks like more work than has to be done.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Feb 17th, 2006
0

Re: conversions to int/double/etc...

Quote originally posted by server_crash ...
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Feb 17th, 2006
0

Re: conversions to int/double/etc...

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: help me on my case study!!!
Next Thread in C Forum Timeline: derived class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC