conversions to int/double/etc...

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

conversions to int/double/etc...

 
0
  #1
Feb 16th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #2
Feb 16th, 2006
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,620
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1492
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

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

 
0
  #3
Feb 16th, 2006
>>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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

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

 
0
  #4
Feb 17th, 2006
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!

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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #5
Feb 17th, 2006
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

Originally Posted by server_crash
Okay, you can STFU!
LoL
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 2,108
Reputation: server_crash is on a distinguished road 
Solved Threads: 18
server_crash server_crash is offline Offline
Postaholic

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

 
0
  #6
Feb 17th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,273
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 378
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #7
Feb 17th, 2006
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,846
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

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

 
0
  #8
Feb 17th, 2006
>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.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC