size_t 和 unsigned int 之间的区别? [英] Difference between size_t and unsigned int?

查看:33
本文介绍了size_t 和 unsigned int 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 size_t 感到很困惑.我在互联网上搜索过,到处都提到 size_t 是无符号类型,因此它只能表示非负值.

I am so confused about size_t. I have searched on the internet and everywhere mentioned that size_t is an unsigned type so, it can represent only non-negative values.

我的第一个问题是:如果只用来表示非负值,为什么不用unsigned int 代替size_t?

My first question is: if it is used to represent only non-negative values, why don't we use unsigned int instead of size_t?

我的第二个问题是:size_tunsigned int 是否可以互换?如果没有,那为什么?

My second question is: are size_t and unsigned int interchangeable or not? If not, then why?

谁能给我一个 size_t 的好例子并简要介绍它的工作原理?

And can anyone give me a good example of size_t and briefly its workings?

推荐答案

如果它是用来表示非负值那么为什么我们不使用 unsigned int 而不是 size_t

因为 unsigned int 不是唯一的无符号整数类型.size_t 可以是 unsigned charunsigned shortunsigned intunsigned long 中的任何一个> 或 unsigned long long,取决于实现.

Because unsigned int is not the only unsigned integer type. size_t could be any of unsigned char, unsigned short, unsigned int, unsigned long or unsigned long long, depending on the implementation.

第二个问题是 size_tunsigned int 是否可以互换,如果不能,那为什么?

Second question is that size_t and unsigned int are interchangeable or not and if not then why?

它们不可互换,原因如上^^.

They aren't interchangeable, for the reason explained above ^^.

谁能给我一个关于 size_t 及其简短工作的好例子?

And can anyone give me a good example of size_t and its brief working ?

我不太明白你所说的它的简短工作"是什么意思.它像任何其他无符号类型一样工作(特别是像它被类型化的类型).鼓励您在描述对象的大小时使用 size_t.特别是sizeof运算符和各种标准库函数,如strlen(),返回size_t.

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, the sizeof operator and various standard library functions, such as strlen(), return size_t.

奖励:这是一篇关于size_t的好文章(以及密切相关的 ptrdiff_t 类型).这很好地说明了为什么您应该使用它.

Bonus: here's a good article about size_t (and the closely related ptrdiff_t type). It reasons very well why you should use it.

这篇关于size_t 和 unsigned int 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆