K
kyagrd
<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
#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