473,405 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Strange result from ptrdiff_t and size_t

<code>
#include <iostream>

int main(void)
{
using namespace std;

int p[100];
int* p1 = p;
int* p11 = p + 2;
int* p2 = p + 12;

ptrdiff_t pd1 = (p2 - p1) * size_t(p11 - p) + (p2-p1) - (p11-p1);
size_t st1 = 30;

cout <<"ptrdiff pd1="<<pd1 <<", size_t st1="<<st1 <<endl;
cout <<"(pd1 < st1)="<<(pd1 < st1) <<endl;
cout <<"(pd1 - st1)="<<(pd1 - st1) <<endl;
cout <<"(st1 - pd1)=" <<(st1 - pd1) <<endl;
cout <<endl;

// for (size_t i=0; i < pd1 - st1 ; ++i) cout <<i << endl;

return 0;
}
</code>

ÀÌ ÇÁ·Î±×·¥ÀÇ ½ÇÇà °á°ú´Â ´ÙÀ½°ú °°½À´Ï´Ù.

<code>
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.

This is not a makeup code but happen to encounter in coding a program.
The C++ Standard Library container has a size() member function
returning size_t, which is an unsigned type, and the subtraction two
between random access iterators returns ptrdiff_t, which is a signed
type. Using such values, some of the for loops went wrong
unexpectedly.

The compiler I used is as following.

<code>
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v --enable-languages=c,c+
+,fortran,objc,obj-c++,treelang --prefix=/usr --enable-shared --with-
system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-
threads=posix --enable-nls --program-suffix=-4.1 --enable-__cxa_atexit
--enable-clocale=gnu --enable-libstdcxx-debug --enable-mpfr --with-
tune=i686 --enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)
</code>

Thanks,

Ahn, Ki Yung

Feb 1 '07 #1
10 3463
ky****@gmail.com 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.

Again, you cannot rely on that because going outside the range of an
integral type is undefined.
Feb 1 '07 #2
On Wed, 31 Jan 2007 21:37:43 -0800, red floyd <no*****@here.dude>
wrote in comp.lang.c++:
ky****@gmail.com 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
Feb 1 '07 #3
red floyd wrote:
ky****@gmail.com 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.
No, ptrdiff_t is signed. Although, in this case its value gets converted
to an unsigned type.
30-34 overflows (underflows?) yielding undefined behavior,
Unsigned integers never overflow.
however, the most common implementation
would wrap around to 0xFFFFFFFC, or 4294967292.
Not just "the most common implementation"; unsigned arithmetic wraps in
this manner on *all* implementations.
Again, you cannot rely on that because going outside the range of an
integral type is undefined.
No, you *can* rely on this behavior as it is guaranteed by the standard.
--
Clark S. Cox III
cl*******@gmail.com
Feb 1 '07 #4
Clark S. Cox III wrote:
red floyd wrote:
>ky****@gmail.com 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.

No, ptrdiff_t is signed. Although, in this case its value gets converted
to an unsigned type.
>30-34 overflows (underflows?) yielding undefined behavior,

Unsigned integers never overflow.
Really? Would you care to reconcile that with 5/5? "If during the
evaluation of an expression, the result is not mathematically defined or
*not in the range of representable values for its type*, the behavior
is undefined" (emphasis mine).

Combine this with 5/9 "if either operand is unsigned, the other shall be
converted to unsigned".

Yes, ptrdiff_t is signed, but size_t is unsigned. Therefore, as Clark
stated, the ptrdiff_t get converted to an unsigned. Then when we take
30-34, section 5/5 kicks in and we have UB.
Feb 1 '07 #5
red floyd wrote:
Clark S. Cox III wrote:
>red floyd wrote:
>>ky****@gmail.com 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.

No, ptrdiff_t is signed. Although, in this case its value gets converted
to an unsigned type.
>>30-34 overflows (underflows?) yielding undefined behavior,

Unsigned integers never overflow.

Really? Would you care to reconcile that with 5/5? "If during the
evaluation of an expression, the result is not mathematically defined or
*not in the range of representable values for its type*, the behavior
is undefined" (emphasis mine).

3.9.1 4:
Unsigned integers, declared unsigned, shall obey the laws of arithmetic
modulo 2 where n is the number of bits in the value representation of
that particular size of integer. 41)

3.9.1 Footnote 41:
This implies that unsigned arithmetic does not overflow because a result
that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value
that can be represented by the resulting unsigned integer type.

--
Clark S. Cox III
cl*******@gmail.com
Feb 1 '07 #6
Clark S. Cox III wrote:
red floyd wrote:
>Clark S. Cox III wrote:
>>red floyd wrote:
ky****@gmail.com 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.
No, ptrdiff_t is signed. Although, in this case its value gets converted
to an unsigned type.

30-34 overflows (underflows?) yielding undefined behavior,
Unsigned integers never overflow.
Really? Would you care to reconcile that with 5/5? "If during the
evaluation of an expression, the result is not mathematically defined or
*not in the range of representable values for its type*, the behavior
is undefined" (emphasis mine).


3.9.1 4:
Unsigned integers, declared unsigned, shall obey the laws of arithmetic
modulo 2 where n is the number of bits in the value representation of
that particular size of integer. 41)

3.9.1 Footnote 41:
This implies that unsigned arithmetic does not overflow because a result
that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value
that can be represented by the resulting unsigned integer type.
That's *OVERFLOW*. What about *UNDERFLOW*?
Feb 1 '07 #7
red floyd wrote:
Clark S. Cox III wrote:
>red floyd wrote:
>>Clark S. Cox III wrote:
red floyd wrote:
ky****@gmail.com 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.
No, ptrdiff_t is signed. Although, in this case its value gets
converted
to an unsigned type.

30-34 overflows (underflows?) yielding undefined behavior,
Unsigned integers never overflow.
Really? Would you care to reconcile that with 5/5? "If during the
evaluation of an expression, the result is not mathematically defined or
*not in the range of representable values for its type*, the behavior
is undefined" (emphasis mine).


3.9.1 4:
Unsigned integers, declared unsigned, shall obey the laws of arithmetic
modulo 2 where n is the number of bits in the value representation of
that particular size of integer. 41)

3.9.1 Footnote 41:
This implies that unsigned arithmetic does not overflow because a result
that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value
that can be represented by the resulting unsigned integer type.

That's *OVERFLOW*. What about *UNDERFLOW*?
The term underflow is only really relevant to floating point types, as
it deals with a result whose magnitude is too small to represent (i.e.
the value is too close to zero). What you're talking about is still
called overflow.
--
Clark S. Cox III
cl*******@gmail.com
Feb 1 '07 #8
red floyd wrote:
Clark S. Cox III wrote:
>red floyd wrote:
>>Clark S. Cox III wrote:
red floyd wrote:
ky****@gmail.com 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.
No, ptrdiff_t is signed. Although, in this case its value gets
converted
to an unsigned type.

30-34 overflows (underflows?) yielding undefined behavior,
Unsigned integers never overflow.
Really? Would you care to reconcile that with 5/5? "If during the
evaluation of an expression, the result is not mathematically defined or
*not in the range of representable values for its type*, the behavior
is undefined" (emphasis mine).


3.9.1 4:
Unsigned integers, declared unsigned, shall obey the laws of arithmetic
modulo 2 where n is the number of bits in the value representation of
that particular size of integer. 41)

3.9.1 Footnote 41:
This implies that unsigned arithmetic does not overflow because a result
that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value
that can be represented by the resulting unsigned integer type.

That's *OVERFLOW*. What about *UNDERFLOW*?
Don't focus on the footnote. The normative clause covers it all because
arithmetic mod 2^n does not care either way. All arithmetic operations with
the exception of division by 0 are well-defined for unsigned interger types
by [3.9.1.4]. (That's why I like them much better than those signed types
that make virtually no guarantees whatsoever.)
Best

Kai-Uwe Bux
Feb 2 '07 #9
Kai-Uwe Bux wrote:
>>>>>30-34 overflows (underflows?) yielding undefined behavior,
Unsigned integers never overflow.
Really? Would you care to reconcile that with 5/5? "If during the
evaluation of an expression, the result is not mathematically defined or
*not in the range of representable values for its type*, the behavior
is undefined" (emphasis mine).

3.9.1 4:
Unsigned integers, declared unsigned, shall obey the laws of arithmetic
modulo 2 where n is the number of bits in the value representation of
that particular size of integer. 41)

3.9.1 Footnote 41:
This implies that unsigned arithmetic does not overflow because a result
that cannot be represented by the resulting unsigned integer type is
reduced modulo the number that is one greater than the largest value
that can be represented by the resulting unsigned integer type.
That's *OVERFLOW*. What about *UNDERFLOW*?

Don't focus on the footnote. The normative clause covers it all because
arithmetic mod 2^n does not care either way. All arithmetic operations with
the exception of division by 0 are well-defined for unsigned interger types
by [3.9.1.4]. (That's why I like them much better than those signed types
that make virtually no guarantees whatsoever.)
OK. I stand corrected. Sorry about being such a pain.
Feb 2 '07 #10
Kai-Uwe Bux wrote:
Don't focus on the footnote. The normative clause covers it all because
arithmetic mod 2^n does not care either way. All arithmetic operations with
the exception of division by 0 are well-defined for unsigned interger types
by [3.9.1.4]. (That's why I like them much better than those signed types
that make virtually no guarantees whatsoever.)
You know, there should be a note in 5/5 to refer to 3.9.1.4.
Feb 2 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: PengYu.UT | last post by:
I'm wondering when I should use ptrdiff_t and size_t instead of int and unsigned int or long and unsigned long? Is there any guideline I should follow? Peng
9
by: PengYu.UT | last post by:
std::size_t and std::ptrdiff_t are defined in <cstddef>. I've noticed that a bunch of old math function in the global namespace are undefined in <cmath> and they are redefined in std namespace. ...
26
by: robertwessel2 | last post by:
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...
31
by: Spiro Trikaliotis | last post by:
Hello, I have a question regarding subtracting a pointer from another one. Assume I have two pointers p1 and p2, which both point to a memory area obtained with malloc(). Assume p1 = p2 + some...
6
by: jubelbrus | last post by:
I'm getting the following error, and from 40 to 300 similar error when I try to compile on mac os x. I believe that it has something to do with STL, because this error occures when I try to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.