Re: Plauger, size_t and ptrdiff_t
- From: Keith Thompson <kst-u@xxxxxxx>
- Date: Fri, 17 Feb 2006 08:14:02 GMT
"robertwessel2@xxxxxxxxx" <robertwessel2@xxxxxxxxx> writes:
In another thread, a poster mentioned the Posix ssize_t definition
(signed version of size_t). My initial reaction was to wonder what the
point of the Posix definition was when ptrdiff_t was already defined as
such.
I got the idea that ptrdiff_t had to be the same size as size_t from
Plauger's "The Standard C Library," where he states "... It is always
the signed type that has the same number of bits as the4 unsigned type
chosen for size_t..." This language would not rule out one being int
and the other long so long as sizeof(int)==sizeof(long) for the
implementation.
Now I can't see anywhere in the standard that would require that, at
least not directly, and it seems that a size_t of unsigned int and a
prtdiff_t of long (where int and long are different sizes) would be
possible. C99 defines SIZE_MAX as being at least 65535, and
PTRDIFF_MIN/MAX as being at least -/+65535.
So do size_t and ptrdiff_t have to be the same size (or base type) or
not?
There's no requirement in the standard for size_t and ptrdiff_t to be
the same size, but I don't know of any implementation where they
differ.
ptrdiff_t is "the signed integer type of the result of subtracting two
pointers"; size_t is "the unsigned integer type of the result of the
sizeof operator".
Suppose a system only supports objects up to 65535 bytes. The sizeof
operator can only yield values from 0 to 65535, so 16 bits are
sufficient, but pointer subtraction for pointers to elements of an
array of 65535 bytes could yield values from -65535 to +65535, so
ptrdiff_t would have to be at least 17 bits.
--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.
- Follow-Ups:
- Re: Plauger, size_t and ptrdiff_t
- From: Jordan Abel
- Re: Plauger, size_t and ptrdiff_t
- From: P.J. Plauger
- Re: Plauger, size_t and ptrdiff_t
- References:
- Plauger, size_t and ptrdiff_t
- From: robertwessel2@xxxxxxxxx
- Plauger, size_t and ptrdiff_t
- Prev by Date: Re: Cannot explain why the date is changed to 1792.
- Next by Date: file into dynamic arrays
- Previous by thread: Re: Plauger, size_t and ptrdiff_t
- Next by thread: Re: Plauger, size_t and ptrdiff_t
- Index(es):
Relevant Pages
|