![]() |
|
#1
|
|||
|
|||
Need help with "atol". Exceptions handling?Hello everyone!
I am using atol to convert a string into a long number and I don't know how to handle the exceptions thrown, when the converted value overflows. I wrote the following program to test the convertion. CPP / C++ / C Code:
I tried something like CPP / C++ / C Code:
Thank you |
|||
|
#2
|
|||
|
|||
Re: Need help with "atol". Exceptions handling?Quote:
In fact, if atol() overflows the result is undefined. So, you simply can't use atol() if you are concerned about overflows. Period. Full stop. As a matter of fact, overflows are not detected (and can not be detected, and never have been detected) for arithmetic operations on C or C++ built-in integer data types (char, short, int, long: signed and unsigned variants of them). This is the source of a number of bugs in software all over the world, and occasionally they result in exploits of the dreaded "buffer overflow" kind. In general if you want to detect overflow for an integer addition, you could do something like the following: CPP / C++ / C Code:
I suggest you look at results when adding two large numbers or one large number and one small number, etc. Note well: This test is valid for addition of two non-negative longs. Furthermore, it is guaranteed only for systems that use 2's complement representation of long ints. The C standard does not specify how integer data types are to be represented, but systems that you are likely to run across these days do use 2's complement. Other cases of interest for addition: If either of the ints is zero, no overflow is possible (obviously). If one of the ints is positive and the other is negative no overflow is possible for addition. If they are both negative, how would the test be affected? Try it--- this program will not detect overflow in this case, but it's easy to make a test that will be valid for this case. How would you test for overflow upon subtraction (where either or both can be negative). For example, let x = LONG_MAX and let y = -5, and calculate x - y. So, for obtaining user input, instead of atol(), you could write your own conversion function (converting the string entered by the user to a long). This function would probably involve addition and multiplication of the decimal digits, so you also have to know how to detect overflow in decimal integer multiplication as well as addition (left as an exercise for the interested reader). You can search for C++ classes that detect integer overflow (something called safe integer classes or safeints or something like that). Regards, Dave p.s. The C standard does not specify the number of bits that are used for longs. Some systems that I currently use have 32-bit longs and some have 64 (and there may be others that I haven't checked lately). The value LONG_MAX and LONG_MIN are macros that give values for each particular system. |
Recent GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Opinion on my code and a c++ class question | FlipNode | C++ Forum | 7 | 07-Feb-2006 09:15 |
| Microsoft Word Event handling help... | Arthur M | MS Visual C++ / MFC Forum | 0 | 03-Jan-2005 21:09 |
| [Library]HTML Class & CGI handling | dsmith | C Programming Language | 0 | 09-Feb-2004 09:51 |
| [script] Handling Error 404 | JdS | PHP Code Library | 0 | 19-Nov-2003 09:22 |
| error handling code.. | daveyp225 | C++ Forum | 0 | 26-Oct-2003 07:46 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The