天天看点

size_t、ptrdiff_t【转】

<a href="http://longzxr.blog.sohu.com/196837377.html">http://longzxr.blog.sohu.com/196837377.html</a>

对于指向同一数组arr[5]中的两个指针之差的验证:

     数组如下:ptr = arr;

-------------------------------------------------------------------------------------------

int _tmain(int argc, _TCHAR* argv[])

{

char arr[5] = {1,2,3,4,5};

char *ptr = arr;

printf("%d\n",&amp;ptr[4]-&amp;ptr[0]);

system("PAUSE");

return 0;

}

运行输出:4

更换为字符数组,测试结果一样。

《C和指针》P110 分析如下:两个指针相减的结果的类型为ptrdiff_t,它是一种有符号整数类型。减法运算的值为两个指针在内存中的距离(以数组元素的长度为单位,而非字节),因为减法运算的结果将除以数组元素类型的长度。所以该结果与数组中存储的元素的类型无关。

size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t.设计size_t就是为了适应多个平台,其引入增强了程序在不同平台上的可移植性。

ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.同上,使用ptrdiff_t来得到独立于平台的地址差值.

size_type是unsigned类型,表示容器中元素长度或者下标,vector&lt;int&gt;::size_type i = 0;

difference_type是signed类型,表示迭代器差距,vector&lt;int&gt;:: difference_type = iter1-iter2.

前二者位于标准类库std内,后二者专为STL对象所拥有。

//=====================================================================================

http://blog.csdn.net/yyyzlf/article/details/6209935

C and C++ define a special type for pointer arithmetic, namely ptrdiff_t, which is a typedef of a platform-specific signed integral type. You can use a variable of type ptrdiff_t to store the result of subtracting and adding pointers.For example:

What are the advantages of using ptrdiff_t? First, the name ptrdiff_t is self-documenting and helps the reader understand that the variable is used in pointer arithmetic exclusively. Secondly, ptrdiff_t is portable: its underlying type may vary across platforms, but you don't need to make changes in the code when porting it.

【新浪微博】 张昺华--sky

【twitter】 @sky2030_

【facebook】 张昺华 zhangbinghua

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

继续阅读