Re: Converting CString to Integer

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Doug Harrison [MVP] (dsh_at_mvps.org)
Date: 10/15/04


Date: Fri, 15 Oct 2004 11:49:34 -0500

Sergey Kochkarev wrote:

>OK, here you are:
>
>bool GetMyUnsigned(const char* c, unsigned& ret_val)
>{
> if(!c || !*c)
> return false;
>
> ret_val = 0;
> unsigned counter = 0;
> do
> {
> int digit = int(*c - '0');
> if(digit < 0 || digit > 9)
> return false;
>
> if(ret_val > 0xFFFFFFFF / 10 - 1)
> return false;
>
> ret_val = ret_val * 10 + digit;
> } while(*(++c));
>
> return true;
>}
>
>Easy, yeah?
>It is much faster than C++ Runtimes.

Did you measure it? And as mentioned in my last message, do you have an
application which calls it so frequently in such a short time that the
"improvement" actually matters?

Can you find the bug in your function? Hint: You might find it easier to
spot if you made it GetMyUnsignedChar and were to see if it can handle all
values of unsigned char.

And BTW, what's this magic number 0xFFFFFFFF? Do you know a portable way to
get the number you need? How about a portable way to find the largest value
of any unsigned type without #including a header file? To follow my hint
given earlier, you'll have to change this number.

What's "easy" is not writing code such as the above at all and using
standard functions like strtol when possible.

>It does not call other functions.
>It does what it should do and nothing else. It does not have atol bug
>(try converting "123213123123243423434234324234553454534" and see the
>incorrect result, should be 0).

There you go. You earlier expressed your disdain for consulting
documentation, and it shows. You think atol should return zero in that case,
when actually the result is undefined, because the result cannot be
represented in a 32 bit long (which is a typical size and the one VC uses).
That's one reason you use strtox when you don't know anything about the
input and you care about error checking.

>Want documentation? Here you are:
>GetMyUnsigned converts string into unsigned (32-bit) integer and
>returns false if the string does not contain unsigned (32-bit) only.

Unfortunately, your function contains a bug and doesn't fulfill that
contract.

>About STL. What implementation you mean?

Where did I say anything about STL?

>The one that is in VC 6.0 is
>not standard, it uses its own subset of the ANSI standard that is
>compatible with the compiler. Many good standard STL programs cannot
>be built with Microsoft's STL and on Microsoft's compiler (for
>example, try playing with Loki by Andrei Alexandrescu). I'm not sure
>this is a bug, but it is surelly not fully tested to be compatible
>with ANSI standards. Can you use untested code for critical
>applications? I'm not sure.

That's a different issue altogether. The functions we're talking about in
this thread were standardized 15 years ago.

>In conclusion. Certainly, you are right, it is not worthy to think
>yourself if somebody thinks for you. Windows 3.1 took 2 Megabytes on
>my hard drive. Windows XP takes 2 Gigabytes - you know why?

If it makes you feel better, by all means, go back to Windows 3.1. Then you
can revel in your 2 MB footprint on perhaps an 80 GB hard drive. :)
Seriously, while efficiency can't be ignored, it's ever more important to
have a sense of proportion.

-- 
Doug Harrison
Microsoft MVP - Visual C++


Relevant Pages

  • Re: #define and (brackets)
    ... Minor compiler vendors are free to join if they are so inclined, ... analysis hasn't changed between the two versions of the standard. ... This bug is a minor bug in an obscure ...
    (microsoft.public.vc.language)
  • Re: Code or compiler bug re: TRANSFER intrinsic?
    ... > of the fortran standard. ... compiler I tried had a bug relating to it somewhere. ... the other is what gets associated with the dummy argument. ...
    (comp.lang.fortran)
  • Re: Buffer overflows and asctime()
    ... to contain a "bug" as others are not. ... standard the year member receives a maximum value. ... will overflow its buffer if confronted with valid inputs. ... So you still do not understand "undefined behavior". ...
    (comp.std.c)
  • Re: Converting CString to Integer
    ... >a lower price and with life time guarantee is better, ... it contained a bad bug. ... Elsewhere, you've tried quoting the C++ Standard, so I ... I showed how to use strtol correctly in an earlier message. ...
    (microsoft.public.vc.mfc)
  • Re: Using STLport with Microsoft Visual C++ 6
    ... >which is compiler vendor and OS independent. ... The STL was an algorithms library invented by Stepanov and Lee. ... So the STL, in a slightly modified form, is part of the C++ standard ... C++ FAQ: http://www.parashift.com/c++-faq-lite/ ...
    (microsoft.public.vc.stl)