Chinaunix首页 | 论坛 | 博客

分类: C/C++

2011-10-04 21:57:40

size_t type

size_t type is a base unsigned integer type of C/C++ language. It is the result of sizeof operator's execution. The type's size is chosen so that it could store the maximum size of a theoretically possible array. On a 32-bit system size_t will take 32 bits, on a 64-bit one 64 bits. In other words, a variable of size_t type can safely store a pointer. The exception is pointers to class functions but this is a special case. size_t type is usually used for loop counters, array indexing and address arithmetic.

The maximum possible value of size_t type is constant SIZE_MAX.

/*size_t类型是一种基于c/c++语言的无符号整形。它是sizeof擦作的返回结果。这个类型的大小被选作理论上可以存储一个最大的可能数组。在32-bit的系统上,size_t取32bit,在64-bit上取64bit。换句话说,size_t可以安全的存储一个指针。指针指向类函数是个例外,但是那是特殊的类型。size_t类型经常用于循环计数,数组索引,还有地址运算

最大可能的size_t值常数SIZE_MAX;

*/

ptrdiff_t type

ptrdiff_t type is a base signed integer type of C/C++ language. The type's size is chosen so that it could store the maximum size of a theoretically possible array. On a 32-bit system ptrdiff_t will take 32 bits, on a 64-bit one 64 bits. Like in size_t, ptrdiff_t can safely store a pointer except for a pointer to a class function. Also, ptrdiff_t is the result of an expression where one pointer is subtracted from the other (ptr1-ptr2). ptrdiff_t type is usually used for loop counters, array indexing, size storage and address arithmetic.

Portability of size_t and ptrdiff_t

/*ptrdiff_t类型是基于c/c++的有符号整形。这个类型可以存储理论上可能的最大可能的数组。在32-bit系统ptrdiff_t取32位,在64-bit取64位。像size_t,ptrdiff_t可以安全的存储一个指针类函数指针例外。ptrdiff_t存储一个指针(ptr1-ptr2)的减法结果。ptrdiff_t类型是经常用作循环计数,数组索引,还有地址运算。

size_t和ptrdiff_t的移植性*/


The code created with the use of size_t and ptrdiff_t types is easy-portable. The size of size_t and ptrdiff_t always coincide with the pointer's size. Because of this, it is these types that should be used as indexes for large arrays, for storage of pointers and pointer arithmetic.

Linux-application developers often use long type for these purposes. Within the framework of 32-bit and 64-bit data models accepted in Linux, this really works. long type's size coincides with the pointer's size. But this code is incompatible with Windows data model and, consequently, you cannot consider it easy-portable. A more correct solution is to use types size_t and ptrdiff_t.

As an alternative to size_t and ptrdiff_t, Windows-developers can use types DWORD_PTR, SIZE_T, SSIZE_T etc. But still it is desirable to confine to size_t and ptrdiff_t types. 

/*用size_t和ptrdiff_t类型的代码是很好移植的。他们的待续奥经常和指针大小有关。因此,这些被用于数组的索引,来存储指针和指针运算。

linux-程序开发经常用long 类型也是这个目的。32-bit和64-bit的数据模型框架内,long 类型的大小和指针的大小有关。但是这些代码在Windows的数据模型上就不兼容因此不能认为它是易移植的。更正确的做法就是用size_t和ptrdiff_t类型。

size_t和ptrdiff_t的替代,W开发者用类型DWORD_PTR和SIZE_T,SSIZE_T等。但是只是在size_t和ptrdiff_t类型是可取的。



阅读(1245) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~