473,614 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=releas e 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 3489
ky****@gmail.co m 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.d ude>
wrote in comp.lang.c++:
ky****@gmail.co m 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.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 1 '07 #3
red floyd wrote:
ky****@gmail.co m 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.co m 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.co m 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.co m 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.co m 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.co m 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

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

Similar topics

6
4559
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
3090
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. I'm wondering why ::size_t and ::ptrdiff_t aren't undefined. Thanks, Peng
26
6692
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 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...
31
10306
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 value c. Now, I want to obtain the difference between the two, that is, the value c. Which type must I use for c? I searched around and did not find a definitive answer (not even in the
6
14012
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 include any STL files, like list in this example. Please help me. c++ code:
0
8179
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8124
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8576
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6087
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4049
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4119
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2565
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1712
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.