On Wed, 31 Jan 2007 21:37:43 -0800, red floyd <>
wrote in comp.lang.c++:
> wrote:
> > [redacted]
> > ptrdiff pd1=34, size_t st1=30
> > (pd1 < st1)=0
> > (pd1 - st1)=4
> > (st1 - pd1)=4294967292
> > </code>
> >
> > With comparison pd1 is not less than st1; pd1 should be greater than
> > st1. However the result of the minus operation is reversed.
> >
> ptrdiff_t and size_t are unsigned. 30-34 overflows (underflows?)
> yielding undefined behavior, however, the most common implementation
> would wrap around to 0xFFFFFFFC, or 4294967292.
No, ptrdiff_t is and always has been a signed type. When you perform
an arithmetic operation between two different types, in this case
ptrdiff_t which is signed and size_t which is unsigned, there will be
a conversion.
If ptrdiff_t could hold all the values of a size_t, which would
basically mean it would be a wider type than size_t, the size_t value
would be converted to ptrdiff_t and the operation would produce a
signed ptrdiff_t result.
Since on this implementation a ptrdiff_t is the same or of lesser rank
the size_t, it is the ptrdiff_t which is converted to the unsigned
size_t type. The result is well defined.
> Again, you cannot rely on that because going outside the range of an
> integral type is undefined.
Again you are incorrect. What you say is true for signed integer
types, but not true for unsigned integer types. There is no such
thing as overflow or underflow for unsigned types.
--
Jack Klein
Home:
http://JK-Technology.Com
FAQs for
comp.lang.c
http://c-faq.com/
comp.lang.c++
http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html