为size_t和unsigned int之间的区别? [英] Difference between size_t and unsigned int?
问题描述
我很困惑为size_t
。我已经搜索在互联网上到处都提到为size_t
是一个无符号的类型,这样,就可以重新present非负值。
我的第一个问题是,如果是用来重新present非负值,我们为什么不使用 unsigned int类型
而不是为size_t
?
我的第二个问题是:为size_t
和 unsigned int类型
互换或不?如果没有,那么为什么呢?
和任何人都可以给我为size_t
的一个很好的例子,其短暂的工作?
如果使用重新present非负值,所以我们为什么不使用
unsigned int类型
而不是为size_t
块引用>由于
unsigned int类型
不是唯一的无符号整数类型。为size_t
可以是任何将unsigned char
,无符号短
,unsigned int类型
,无符号长
或无符号长长
,视就执行。
第二个问题是,为size_t和unsinged INT可以互换与否,如果没有,为什么?
块引用>他们是不能互换的,对于上面的原因解释
^ ^
。
和任何人都可以给我为size_t的一个很好的例子,其短暂的工作?
块引用>我不太让你的它短暂的工作的意思。它的工作原理是任何其他无符号类型(尤其是,像类型它typedeffed到)。我们鼓励您使用
为size_t
当你描述一个物体的大小。尤其是,的sizeof
运营商和各类标准库函数,如的strlen()
,返回为size_t
。奖励:这里是一个很好的文章(文章删除,请使用<一个href=\"http://web.archive.org/web/20140828142605/http://www.$c$cproject.com/Articles/60082/About-size_t-and-ptrdiff_t\">wayback机快照)关于
为size_t
(以及与之密切相关的ptrdiff_t的
类型)。原因吧非常好,为什么你应该使用它。I am so confused about
size_t
. I have searched on the internet and everywhere mentioned thatsize_t
is an unsigned type so, it can represent non-negative value.My first question is that if it is uses to represent non negative values, why don't we use
unsigned int
instead ofsize_t
?My second question is: are
size_t
andunsigned int
interchangeable or not? If not then why?And can anyone give me a good example of
size_t
and its brief working?解决方案if it is use to represent non negative value so why we not using
unsigned int
instead ofsize_t
Because
unsigned int
is not the only unsigned integral type.size_t
could be any ofunsigned char
,unsigned short
,unsigned int
,unsigned long
orunsigned long long
, depending on the implementation.Second question is that size_t and unsinged int are interchangable or not and if not then why?
They aren't interchangeable, for the reason explained above
^^
.And can anyone give me a good example of size_t and its brief working ?
I don't quite get what you mean by "its brief working". It works like any other unsigned type (in particular, like the type it's typedeffed to). You are encouraged to use
size_t
when you are describing the size of an object. In particular, thesizeof
operator and various standard library functions, such asstrlen()
, returnsize_t
.Bonus: here's a good article (article deleted, use wayback machine snapshot) about
size_t
(and the closely relatedptrdiff_t
type). It reasons very well why you should use it.这篇关于为size_t和unsigned int之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!