Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Finding the max value of a typedefed type

4 views
Skip to first unread message

Kevin Goodsell

unread,
May 7, 2002, 1:47:06 PM5/7/02
to
Is the following code safe?

typedef unsigned int my_type;
#define MY_TYPE_MAX ((my_type)(-1))

The goal is to be able to change the type of my_type and have
MY_TYPE_MAX automatically be correct. I am assuming that my_type will
be unsigned. Any better ideas? Thanks.

-Kevin

Mathew Hendry

unread,
May 7, 2002, 2:05:47 PM5/7/02
to
On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
wrote:

So long as my_type is unsigned, you're safe.

-- Mat.

Emmanuel Delahaye

unread,
May 7, 2002, 3:23:16 PM5/7/02
to
In 'comp.lang.c', good...@bridgernet.com (Kevin Goodsell) wrote in
news:2fa5ba58.02050...@posting.google.com:

It's fine. A pair of parens is redundent:

#define MY_TYPE_MAX ((my_type)-1)

How about a generic:

#define MY_MAX_OF(T) ((T)-1)

--
-ed- emdel at noos.fr
The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
C-library: http://www.dinkumware.com/htm_cl/index.html
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/

Martin F.

unread,
May 7, 2002, 4:40:30 PM5/7/02
to
"Mathew Hendry" <mathew...@hotmail.com> wrote in message
news:cq5gduc4n2f6roamu...@4ax.com...

But how can I find out the maximum value of a signed type?
I have thought about that and found no really correct solution.


Gergo Barany

unread,
May 7, 2002, 5:16:06 PM5/7/02
to
Martin F. <RFr...@t-online.de> wrote:
> "Mathew Hendry" <mathew...@hotmail.com> wrote in message
> news:cq5gduc4n2f6roamu...@4ax.com...
> > On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
> > wrote:
> > >Is the following code safe?
> > >
> > >typedef unsigned int my_type;
> > >#define MY_TYPE_MAX ((my_type)(-1))
> >
> > So long as my_type is unsigned, you're safe.
>
> But how can I find out the maximum value of a signed type?
> I have thought about that and found no really correct solution.

There is no safe way to compute that because signed integer overflow
invokes undefined behavior.
Two theoretically unsafe methods that might work in the Real World are
simply taking Utype_MAX / 2, or incrementing a variable until it wraps
around, then decrementing it by one.
The best way, however, is to provide wrappers for the appropriate
<limits.h> macros.


Gergo
--
Weekend, where are you?

CBFalconer

unread,
May 7, 2002, 6:31:43 PM5/7/02
to

All defined in limits.h
--
Chuck F (cbfal...@yahoo.com) (cbfal...@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!


Mathew Hendry

unread,
May 7, 2002, 7:45:30 PM5/7/02
to
On Tue, 7 May 2002 22:40:30 +0200, "Martin F." <RFr...@t-online.de>
wrote:

>"Mathew Hendry" <mathew...@hotmail.com> wrote in message
>news:cq5gduc4n2f6roamu...@4ax.com...
>
>> On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
>> wrote:
>>
>> >Is the following code safe?
>> >
>> >typedef unsigned int my_type;
>> >#define MY_TYPE_MAX ((my_type)(-1))
>>

>> So long as my_type is unsigned, you're safe.
>
>But how can I find out the maximum value of a signed type?
>I have thought about that and found no really correct solution.

There isn't one. You could assume that all types of a given size have
the same range, and choose constants from <limits.h> accordingly, but
I'll wager there are machines where that wouldn't work.

-- Mat.

Dan Pop

unread,
May 8, 2002, 10:25:54 AM5/8/02
to

It's guaranteed to be one less than a power of two. You can try to
store such values in an object of type my_type and then compare the
value stored in the object with the one that was assigned to it. The
largest value that compares equal is the one you're looking for.

This approach is safe in C89 but *theoretically* unsafe in C99. For
practical reasons, you can safely expect it to work in C99, too.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan...@ifh.de

Dan Pop

unread,
May 8, 2002, 10:28:17 AM5/8/02
to
In <slrnadgh0l...@hold.otthon.at> Gergo Barany <ge...@tud.at> writes:

>Martin F. <RFr...@t-online.de> wrote:
>> "Mathew Hendry" <mathew...@hotmail.com> wrote in message
>> news:cq5gduc4n2f6roamu...@4ax.com...
>> > On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
>> > wrote:
>> > >Is the following code safe?
>> > >
>> > >typedef unsigned int my_type;
>> > >#define MY_TYPE_MAX ((my_type)(-1))
>> >
>> > So long as my_type is unsigned, you're safe.
>>
>> But how can I find out the maximum value of a signed type?
>> I have thought about that and found no really correct solution.
>
>There is no safe way to compute that because signed integer overflow
>invokes undefined behavior.

But you don't need to generate signed integer overflow in the process
of computing it. It can be done exclusively with assignments, which
are safe in C89.

Dan Pop

unread,
May 8, 2002, 10:36:01 AM5/8/02
to
In <3CD8518E...@yahoo.com> CBFalconer <cbfal...@yahoo.com> writes:

>"Martin F." wrote:
>>
>> "Mathew Hendry" <mathew...@hotmail.com> wrote in message
>> news:cq5gduc4n2f6roamu...@4ax.com...
>> > On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
>> > wrote:
>> >
>> > >Is the following code safe?
>> > >
>> > >typedef unsigned int my_type;
>> > >#define MY_TYPE_MAX ((my_type)(-1))
>> > >
>> > >The goal is to be able to change the type of my_type and have

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


>> > >MY_TYPE_MAX automatically be correct. I am assuming that my_type will

^^^^^^^^^^^^=============^^^^^^^^^^^


>> > >be unsigned. Any better ideas? Thanks.
>> >
>> > So long as my_type is unsigned, you're safe.
>>
>> But how can I find out the maximum value of a signed type?
>> I have thought about that and found no really correct solution.
>
>All defined in limits.h

It doesn't hurt to engage your brain before your fingers.

<limits.h> is of little help if the actual type definition is not
known when the code is written. Think about a C89 program that needs
to be able to figure out PTRDIFF_MAX.

Eric Sosman

unread,
May 8, 2002, 10:35:04 AM5/8/02
to
CBFalconer wrote:
>
> "Martin F." wrote:
> >
> > "Mathew Hendry" <mathew...@hotmail.com> wrote in message
> > news:cq5gduc4n2f6roamu...@4ax.com...
> > > On 7 May 2002 10:47:06 -0700, good...@bridgernet.com (Kevin Goodsell)
> > > wrote:
> > >
> > > >Is the following code safe?
> > > >
> > > >typedef unsigned int my_type;
> > > >#define MY_TYPE_MAX ((my_type)(-1))
> > > >
> > > >The goal is to be able to change the type of my_type and have
> > > >MY_TYPE_MAX automatically be correct. I am assuming that my_type will
> > > >be unsigned. Any better ideas? Thanks.
> > >
> > > So long as my_type is unsigned, you're safe.
> >
> > But how can I find out the maximum value of a signed type?
> > I have thought about that and found no really correct solution.
>
> All defined in limits.h

Not quite. What, for example, is the maximum value of
a `time_t'? Of a `clock_t'?

--
Eric....@sun.com

rihad

unread,
May 8, 2002, 12:11:41 PM5/8/02
to
On 7 May 2002 19:23:16 GMT, Emmanuel Delahaye <emdel...@noos.fr>
wrote:

>In 'comp.lang.c', good...@bridgernet.com (Kevin Goodsell) wrote in
>news:2fa5ba58.02050...@posting.google.com:
>
>> Is the following code safe?
>>
>> typedef unsigned int my_type;
>> #define MY_TYPE_MAX ((my_type)(-1))
>>

>How about a generic:
>
>#define MY_MAX_OF(T) ((T)-1)

Great as long as you're cautious. One can be too easily misled into
thinking that it's a safe "operator" by now and write code like

type_t var;
. . .
size_t n = MY_MAX_OF(var);

Jim Cook

unread,
May 8, 2002, 12:25:54 PM5/8/02
to
> typedef unsigned int my_type;
> #define MY_TYPE_MAX ((my_type)(-1))

I don't believe the standard requires a two's complement internal
format. If this were on a one's complement 8-bit machine, negative one
would be represented as binary 10000001, far from the max unsigned
value.

--
jc...@strobedata.com Live Honourably 4/1 - 4/3 + 4/5 - 4/7 + . . .
2002 Thu: Feb/last 4/4 6/6 8/8/ 10/10 12/12 9/5 5/9 7/11 11/7 3/14
Strobe Data Inc. home page http://www.strobedata.com
My home page O- http://jcook.net

CBFalconer

unread,
May 8, 2002, 1:50:53 PM5/8/02
to
Jim Cook wrote:
>
> > typedef unsigned int my_type;
> > #define MY_TYPE_MAX ((my_type)(-1))
>
> I don't believe the standard requires a two's complement internal
> format. If this were on a one's complement 8-bit machine, negative
> one would be represented as binary 10000001, far from the max
> unsigned value.

Hardly. You described sign/magnitude. It would be 11111110.
However the conversion to unsigned range has to follow 2's
complement rules, and so is not affected by the underlying
arithmetic system.

Gergo Barany

unread,
May 8, 2002, 1:54:51 PM5/8/02
to

How do you generate the numbers to be assigned? How are these numbers
converted if they are not in the range representable by the target? Is
this different in C99?


Gergo
--
Everyone is a genius. It's just that some people are too stupid to
realize it.

Eric Sosman

unread,
May 8, 2002, 1:29:05 PM5/8/02
to
Jim Cook wrote:
>
> > typedef unsigned int my_type;
> > #define MY_TYPE_MAX ((my_type)(-1))
>
> I don't believe the standard requires a two's complement internal
> format. If this were on a one's complement 8-bit machine, negative one
> would be represented as binary 10000001, far from the max unsigned
> value.

Since `my_type' is known to be an unsigned type, none
of this makes any difference. Remember, a cast operator
*converts* the *value* of its operand from one type to
another; it does *not* say "Interpret this bag of bits as
if they were of a specified type." In the expression
above, the *value* negative one, no matter what bit pattern
it has, is *converted* to `my_type', and by the rules of
unsigned arithmetic this necessarily produces the maximum
possible `my_type' value consisting entirely of 1-bits.

--
Eric....@sun.com

Jim Cook

unread,
May 8, 2002, 4:53:44 PM5/8/02
to
> > > typedef unsigned int my_type;
> > > #define MY_TYPE_MAX ((my_type)(-1))

> > format. If this were on a one's complement 8-bit machine, negative


> > one would be represented as binary 10000001, far from the max

> Hardly. You described sign/magnitude. It would be 11111110.


> However the conversion to unsigned range has to follow 2's
> complement rules, and so is not affected by the underlying

Arghhhh. I can't believe I made a mistake of that ... er ... magnitude.


"The C Programming Language, Kernighan and Ritchie" says:

"A6.2 Integral Conversions
"Any integer is converted to a given unsigned type by finding the
smallest non-negative value that is congruent to that integer, modulo
one more than the largest value that can be represented in the unsigned
type."

So, to be nitpicky (a tenuous position for me to take at this time, I
grant), it's not following 2's complement rules, but simple arithmetic.
It amounts to producing the proper answer, though.

A negative one, modulo one more than the largest value ... will clearly
give the largest value. By definition.

I stand corrected on what a type cast does.

Peter Nilsson

unread,
May 8, 2002, 11:43:18 PM5/8/02
to
Gergo Barany <ge...@tud.at> wrote in message news:<slrnadipjb...@hold.otthon.at>...

> Dan Pop <Dan...@ifh.de> wrote:
> > In <slrnadgh0l...@hold.otthon.at> Gergo Barany <ge...@tud.at> writes:
> > >Martin F. <RFr...@t-online.de> wrote:
> > >> But how can I find out the maximum value of a signed type?
> > >> I have thought about that and found no really correct solution.
> > >
> > >There is no safe way to compute that because signed integer overflow
> > >invokes undefined behavior.
> >
> > But you don't need to generate signed integer overflow in the process
> > of computing it. It can be done exclusively with assignments, which
> > are safe in C89.
>
> How do you generate the numbers to be assigned? How are these numbers
> converted if they are not in the range representable by the target? Is
> this different in C99?

I believe the conversion of unsigned to signed is implementation
defined if the value is not representable for the target type. But
since positive [signed] numbers must have the same representation as
their unsigned counterparts [except possibly 0!] I believe the
following might work for signed "mystery" integers:

mystery mystery_max(void)
{
uintmax_t prev, curr; /* or unsigned long for C90 say? */
mystery best = 0;

for (prev = 63; (curr = 2*prev + 1) != prev; prev = curr)
if ((mystery) curr > best)
best = curr;

return best;
}

--
Peter

Dan Pop

unread,
May 10, 2002, 11:15:44 AM5/10/02
to
In <slrnadipjb...@hold.otthon.at> Gergo Barany <ge...@tud.at> writes:

>Dan Pop <Dan...@ifh.de> wrote:
>> In <slrnadgh0l...@hold.otthon.at> Gergo Barany <ge...@tud.at> writes:
>> >Martin F. <RFr...@t-online.de> wrote:
>> >> But how can I find out the maximum value of a signed type?
>> >> I have thought about that and found no really correct solution.
>> >
>> >There is no safe way to compute that because signed integer overflow
>> >invokes undefined behavior.
>>
>> But you don't need to generate signed integer overflow in the process
>> of computing it. It can be done exclusively with assignments, which
>> are safe in C89.
>
>How do you generate the numbers to be assigned?

Using unsigned arithmetic on the widest unsigned standard type.

>How are these numbers
>converted if they are not in the range representable by the target?

In an implementation-defined manner.

>Is this different in C99?

C99 apparently allows the conversion to raise a signal, if the value is
out of range. This is what makes the approach theoretically unsafe in
C99.

Lawrence Kirby

unread,
May 12, 2002, 12:11:29 PM5/12/02
to
On Wednesday, in article <3CD93798...@sun.com>
Eric....@sun.com "Eric Sosman" wrote:

...

>> All defined in limits.h
>
> Not quite. What, for example, is the maximum value of
>a `time_t'? Of a `clock_t'?

Tricky, since these can have floating point types.

--
-----------------------------------------
Lawrence Kirby | fr...@genesis.demon.co.uk
Wilts, England | 7073...@compuserve.com
-----------------------------------------

0 new messages