[英]converting from size_t to unsigned int
Is it possible that converting from size_t to unsigned int result in overflow . 从size_t转换为unsigned int是否有可能导致溢出。
size_t x = foo ( ) ; // foo ( ) returns a value in type size_t
unsigned int ux = (unsigned int ) x ;
ux == x // Is result of that line always 1 ?
language : c++ 语言:C ++
platform : any 平台:任何
Yes it's possible, size_t
and int
don't necessarily have the same size. 是的,有可能,
size_t
和int
不一定具有相同的大小。 It's actually very common to have 64bit size_t
s and 32bit int
s. 具有64位的
size_t
和32位的int
实际上是非常普遍的。
C++11 draft N3290 says this in §18.2/6: C ++ 11草案N3290在§18.2/ 6中对此进行了说明:
The type size_t is an implementation-defined unsigned integer type that is large enough to contain the size in bytes of any object.
size_t类型是实现定义的无符号整数类型,该类型足够大以包含任何对象的字节大小。
unsigned int
on the other hand is only required to be able to store values from 0 to UINT_MAX
(defined in <climits>
and following the C standard header <limits.h>
) which is only guaranteed to be at least 65535 (2 16 -1). 另一方面,仅要求
unsigned int
能够存储从0到UINT_MAX
值(在<climits>
定义,并遵循C标准头文件<limits.h>
),只能保证至少为65535(2 16- 1)。
Yes, overflow can occur on some platforms. 是的,在某些平台上可能会发生溢出。 For example,
size_t
can be defined as unsigned long
, which can easily be bigger than unsigned int
. 例如,可以将
size_t
定义为unsigned long
,它可以轻松地大于unsigned int
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.