| | |
conversions to int/double/etc...
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
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.
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.
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.
•
•
Join Date: Jun 2004
Posts: 2,108
Reputation:
Solved Threads: 18
•
•
•
•
Originally Posted by Ancient Dragon
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.
Thanks Narue.
•
•
•
•
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.
•
•
•
•
Originally Posted by server_crash
Okay, you can STFU!
*Voted best profile in the world*
•
•
•
•
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.
*Voted best profile in the world*
>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.
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
![]() |
Similar Threads
- Conversions (C++)
- fstream to char and int array (C++)
Other Threads in the C Forum
- Previous Thread: help me on my case study!!!
- Next Thread: Difference between calloc and malloc
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling scripting segmentationfault send shape socketprograming spoonfeeding stack standard string strings structures student suggestions systemcall test testautomation unix user variable voidmain() wab win32api windows.h






