size_t(-1) / sizeof(_Tp)到底是何意义?

来源:互联网 发布:品茗软件电话 编辑:程序博客网 时间:2024/05/15 09:11

Ø  size_t(-1)/sizeof(_Tp)到底是什么意义?

32位机来说,它代表了实现/平台允许你放入容器的最大元素数目,对于std::vector<T>最大容量为2^32 / sizeof(T) 个元素。换句话来说, 也就是我能完全使用4GiB虚拟内存地址空间(实际不可能)而填充的最大元素数目。

Ø  为什么size_t(-1)2^32

size_t通常在32位机上定义为32位,而在64位机上定义为64位的无符号整型。通过强制类型转换,size_t(-1)恰能代表其能表示的最大值,即2^32 (2^64).

 

英文资料:Type size_t 来源:维基百科 http://en.wikipedia.org/wiki/Stddef.h

The type size_t represents the appropriate type for representing the size of objects of memory areas, and for use in dereferencing the elements of an array. It has an implementation-dependent size; usually but not necessarily, it has a 32-bit representation on 32-bit systems and a 64-bit representation on 64-bit systems. It is unsigned.

 

 

 

 

 

原创粉丝点击