K&R 2 exercise 2-3

M

Martin

Richard Bos said:
Why do work which the library will do for you?

#include <limits.h>
#include <stdlib.h>

unsigned int htoi(char *txt)
{
unsigned long tmp=strtoul(txt, 0, 16);
if (tmp>UINT_MAX) return UINT_MAX;
return tmp;
}

Shorter, sweeter, doesn't waste value space on
negative numbers which you don't parse anyway,
doesn't invoke undefined behaviour on overflow,
and is portable no matter what your character set it.
Feel free.

Isn't there a possibility that UINT_MAX == ULONG_MAX?

Martin
http://martinobrien.co.uk/
 
R

RoRsOaMrPiEo

so your is htou and not htoi
is there any pc where
(int) (unsigned) INT_MAX != INT_MAX (this would be true for K&R?)
or where
(int) (unsigned) INT_MIN != INT_MIN

or where if zE{INT_MIN...INT_MAX}
then (int) (unsigned) z != z
 
C

CBFalconer

Martin said:
If UINT_MAX == ULONG_MAX then tmp>UINT_MAX can never be true and
UINT_MAX will never be returned by your function, even when
overflow occurs.

You snipped the code in question and the attributions (it wasn't
my function), but strtoul will return ULONG_MAX for an overflow,
IIRC, which in this case is == UINT_MAX, and is returned. The
code is portable.

I repeat - so?
 
N

nrk

Martin said:
If UINT_MAX == ULONG_MAX then tmp>UINT_MAX can never be true and UINT_MAX
will never be returned by your function, even when overflow occurs.

That would automatically be taken care of by strtoul. In case of overflow,
strtoul will return ULONG_MAX (which also happens to UINT_MAX in the
scenario you describe) and set errno to ERANGE. Richard's code takes the
extra care for situations where ULONG_MAX > UINT_MAX.

-nrk.
 
R

Richard Bos

RoRsOaMrPiEo said:
so your is htou and not htoi

If you wish. Yours doesn't read signed hex numbers, either, though, so I
fail to see the advantage of having a signed format. If you wish, you
can remove all unsigneds, use strtol() instead of strtoul(), and INT_MAX
instead of UINT_MAX, and you'll have a version which can read only half
of the original numbers but returns a signed int.

In fact, if I were you, I'd use strtoul() directly. Much simpler.
crash where in code?

Wherever it invokes undefined behaviour. I haven't checked it in detail,
but people upthread said that it does. They may, of course, have been
mistaken.

Richard
 
R

Richard Bos

RoRsOaMrPiEo said:
is there any pc where
(int) (unsigned) INT_MAX != INT_MAX (this would be true for K&R?)

No. INT_MAX must be representable in unsigned int (6.2.5#9 in C99).
(int) (unsigned) INT_MIN != INT_MIN

or where if zE{INT_MIN...INT_MAX}
then (int) (unsigned) z != z

Whether such a machine actually exists I don't know, but AFAICT it's
certainly allowed. Conversion of out-of-range values to unsigned format
is defined to be done modulo (<type>_MAX+1), but conversion of out-of-
range values to signed format is not well-defined.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top