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

本文解释了size_t(-1)/sizeof(_Tp)在32位和64位系统中所代表的意义,即实现/平台允许的最大容器元素数量。同时介绍了size_t(-1)为何等于2^32(或2^64),并讨论了如何利用这些信息来确定容器如std::vector<T>的最大容量。

Ø  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.

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值