SIZE_MAX under c89

A

aegis

I've looked through the c89 standard and I do not see SIZE_MAX
anywhere. So this was a new addition to c99? Isn't it legal, since c89
does not define SIZE_MAX, to just use #define SIZE_MAX (size_t)-1 ?
 
G

goose

aegis said:
I've looked through the c89 standard and I do not see SIZE_MAX
anywhere. So this was a new addition to c99? Isn't it legal, since c89
does not define SIZE_MAX, to just use #define SIZE_MAX (size_t)-1 ?

do you mean
#define SIZE_MAX (sizeof (size_t) -1)

?
 
C

Coos Haak

Op 5 Jun 2005 15:05:25 -0700 schreef aegis:
I've looked through the c89 standard and I do not see SIZE_MAX
anywhere. So this was a new addition to c99? Isn't it legal, since c89
does not define SIZE_MAX, to just use #define SIZE_MAX (size_t)-1 ?

I don't know the answer to your first question, I don't have c89.
The #define seems wrong, SIZE_MAX is a constant, size_t is a type.
Perhaps #define SIZE_MAX UINT_MAX will do.
 
K

Keith Thompson

Coos Haak said:
Op 5 Jun 2005 15:05:25 -0700 schreef aegis:


I don't know the answer to your first question, I don't have c89.
The #define seems wrong, SIZE_MAX is a constant, size_t is a type.
Perhaps #define SIZE_MAX UINT_MAX will do.

No, #define SIZE_MAX UINT_MAX won't do, unless size_t happens to have
the same range as unsigned int.

I believe SIZE_MAX is new in C99.

The suggested definition
#define SIZE_MAX (size_t)-1
defines SIZE_MAX as the value -1, converted to type size_t. I would
put parentheses around the definition:
#define SIZE_MAX ((size_t)-1)
It should give you the correct value on most or all systems. (There
may be some issues if size_t has padding bits; I'm too lazy to track
down the details.)
 
P

pete

Keith said:
I would
put parentheses around the definition:
#define SIZE_MAX ((size_t)-1)

In case of maybe C99 compiler:

#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif
It should give you the correct value on most or all systems. (There
may be some issues if size_t has padding bits; I'm too lazy to track
down the details.)

I don't know what issues you mean.
Do you know the rules for converting negative integer values
to unsigned type values?
 
B

bjrnove

pete said:
In case of maybe C99 compiler:

#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif


I don't know what issues you mean.
Do you know the rules for converting negative integer values
to unsigned type values?

This asumes that size_t is unsigned. Will size_t always be unsigned? If
not the size will actually be -1 and not the capacity of size_t.
 
M

Martin Ambuhl

bjrnove said:
This asumes that size_t is unsigned. Will size_t always be unsigned? If
not the size will actually be -1 and not the capacity of size_t.

Yes, size_t is unsigned by definition. From the standard:

7.17 Common definitions <stddef.h>
[...]
size_t

which is the unsigned integer type of the result of the
sizeof operator;
 
C

CBFalconer

bjrnove said:
.... snip ...

This asumes that size_t is unsigned. Will size_t always be unsigned?
If not the size will actually be -1 and not the capacity of size_t.

Yes, because the standard says it is unsigned.
 
K

Keith Thompson

pete said:
In case of maybe C99 compiler:

#ifndef SIZE_MAX
#define SIZE_MAX ((size_t)-1)
#endif

That's good.
I don't know what issues you mean.
Do you know the rules for converting negative integer values
to unsigned type values?

Usually I do, but they had slipped my mind when I wrote the above, and
I didn't have my copy of the standard handy. You're right,
((size_t)-1) is guaranteed to be the maximum value that can be
represented in type size_t; padding bits and other representation
considerations are irrelevant. (The same thing works for any unsigned
type.)
 
E

Eric Sosman

aegis said:
I've looked through the c89 standard and I do not see SIZE_MAX
anywhere. So this was a new addition to c99? Isn't it legal, since c89
does not define SIZE_MAX, to just use #define SIZE_MAX (size_t)-1 ?

`SIZE_MAX' and `(size_t)-1' produce the same value, but
in different forms. `SIZE_MAX' is in a form suitable for
use in #if directives, but the preprocessor cannot evaluate
`(size_t)-1'. (The preprocessor cannot evaluate casts,
because the preprocessor operates at a stage of compilation
before types themselves have come into existence.)
 

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

Similar Threads


Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top