为size_t [英] size_t

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

问题描述



当我应该使用size_t而不是例如int时,有人可以给我一些关于

的经验法则吗?


在所有

系统中,size_t *总是* typedef'作为最大的无符号整数类型吗?


我知道strlen()函数并且这样返回一个size_t,

并且它应该以这种方式接收。


我当时正在创建某种密码库。我的所有

API方法都应该使用size_t参数instad例如int参数,

以及为什么?


And..Should我在stdlib函数中使用size_t等其他地方的麻烦

需要/返回我的库? (如果是,为什么?)


我很烦,因为我的代码现在适用于linux,sun solaris和windoze(32

位),我不喜欢不想在64位上运行它的奇怪问题

架构。


最好的问候

Steffen


Can somebody please give me some rules of thumb about
when I should be using size_t instead of for example int ?

Is size_t *always* typedef''d as the largest unsigned integral type on all
systems ?

I know that the strlen() functions and such returns a size_t,
and it should be received that way.

I am at the time creating some sort of cryptolibrary. Should all my
API methods use size_t parameters instad of for example int parameters,
and why ?

And..Should I bother using size_t other places than when stdlib functions
need/return then in my library? (If yes, why?)

I bother because my code now works on linux,sun solaris and windoze (32
bits), and I don''t want strange problems running it on 64 bit
architectures.

Best regards
Steffen

推荐答案

Steffen Fiksdal< st ****** @ ulrik.uio.no>写道:
Steffen Fiksdal <st******@ulrik.uio.no> writes:
当我应该使用size_t而不是例如int时,有人可以给我一些关于
的经验法则吗?


使用size_t来表示对象的大小。

is size_t *总是* typedef'作为所有
系统?


编号(这是uintmax_t,至少在C99系统上。)

我当时正在创建某种密码库。我的所有
API方法都应该使用size_t参数instad例如int参数,
为什么?
Can somebody please give me some rules of thumb about
when I should be using size_t instead of for example int ?
Use size_t to represent the size of an object.
Is size_t *always* typedef''d as the largest unsigned integral type on all
systems ?
No. (That''s uintmax_t, at least on C99 systems.)
I am at the time creating some sort of cryptolibrary. Should all my
API methods use size_t parameters instad of for example int parameters,
and why ?




size_t是代表

对象的大小。如果你代表一个尺寸,使用size_t,否则

选择另一种合适的类型。


简单地搜索和替换` int''by

`size_t''。

-

我没有C& V这个方便,但我有Dan Pop。

--E。 Gibbons



size_t is the appropriate type to represent the size of an
object. If you''re representing a size, use size_t, otherwise
pick another appropriate type.

It is not appropriate to simply search-and-replace `int'' by
`size_t''.
--
"I don''t have C&V for that handy, but I''ve got Dan Pop."
--E. Gibbons


Steffen Fiksdal写道:
Steffen Fiksdal wrote:
当我应该使用size_t代替时,有人可以给我一些关于
的经验法则吗?例如int?


一般来说,每当你描述

大小的东西时,都要使用size_t。有些人还建议使用size_t

作为数组索引,但这有时不方便。

is size_t *总是* typedef'作为所有<的最大无符号整数类型系统?


否;可能存在比任何可能的对象更大的整数。

例如,你会发现size_t为32位的系统,但是

unsigned long long是64.

我知道strlen()函数等返回一个size_t,
它应该以这种方式接收。

我当时正在创建某种密码库。我的所有
API方法都应该使用size_t参数instad,例如int参数,
为什么?


是的,可能。为什么?因为int可能无法
描述缓冲区的大小以及你没有传回的内容

及以上。请记住,一个int可以缩小到16位。

而且......我是否应该使用size_t其他地方而不是stdlib函数
需要/返回我的库? (如果是,为什么?)


将size_t用于对象大小,数组大小等。如果

你正在处理可能超过32767

元素的数组,请使用size_t数组索引。

我很烦,因为我的代码现在适用于linux,sun solaris和windoze(32位),我不想在64位
架构上运行它时出现奇怪的问题。
Can somebody please give me some rules of thumb about
when I should be using size_t instead of for example int ?
Generally, use a size_t whenever you''re describing the
size of something. Some people also recommend using size_t
for array indices, but this is sometimes inconvenient.
Is size_t *always* typedef''d as the largest unsigned integral type on all
systems ?
No; there may be integers larger than any possible object.
For example, you''ll find systems where size_t is 32 bits but
unsigned long long is 64.
I know that the strlen() functions and such returns a size_t,
and it should be received that way.

I am at the time creating some sort of cryptolibrary. Should all my
API methods use size_t parameters instad of for example int parameters,
and why ?
Yes, probably. Why? Because an int may not be able to
describe the sizes of the buffers and what-not you pass back
and forth. Remember, an int could be as narrow as 16 bits.
And..Should I bother using size_t other places than when stdlib functions
need/return then in my library? (If yes, why?)
Use size_t for object sizes, array sizes, and so on. If
you''re dealing with arrays that might have more than 32767
elements, use size_t array indices.
I bother because my code now works on linux,sun solaris and windoze (32
bits), and I don''t want strange problems running it on 64 bit
architectures.




在适当的地方使用size_t可以避免某些类型的b / b
麻烦。转过来问题并以这种方式思考:

您是否会使用signed char来描述缓冲区大小?如果没有,

为什么不呢?


-
Er ********* @ sun.com



Using size_t where appropriate will avoid some kinds of
trouble. Turn the question around and think of it this way:
Would you use signed char to describe buffer sizes? If not,
why not?

--
Er*********@sun.com


Steffen Fiksdal< st *** ***@ulrik.uio.no>写道:
Steffen Fiksdal <st******@ulrik.uio.no> writes:
当我应该使用size_t而不是例如int时,有人可以给我一些关于
的经验法则吗?

是size_t *总是* typedef所有
系统中最大的无符号整数类型?


编号size_t是一个无符号类型,能够表示对象的大小(

字节)(尽管可以想象可能存在

对象size_t太大了)。例如,32位系统可能具有32位大小的b $ b,但是64位long long。

我知道strlen()函数和这样返回一个size_t,
就应该以这种方式接收。

我当时正在创建某种密码库。我的所有
API方法都应该使用size_t参数instad,例如int参数,
为什么?

并且......我应该使用size_t其他地方而不是stdlib函数
需要/返回我的库? (如果是,为什么?)

我很烦,因为我的代码现在适用于linux,sun solaris和windoze(32位),我不想在运行它时遇到奇怪的问题64位
架构。
Can somebody please give me some rules of thumb about
when I should be using size_t instead of for example int ?

Is size_t *always* typedef''d as the largest unsigned integral type on all
systems ?
No. size_t is an unsigned type capable of representing the size (in
bytes) of an object (though it''s conceivable that there could be
objects too big for size_t). For example, a 32-bit system could have
a 32-bit size_t but a 64-bit "long long".
I know that the strlen() functions and such returns a size_t,
and it should be received that way.

I am at the time creating some sort of cryptolibrary. Should all my
API methods use size_t parameters instad of for example int parameters,
and why ?

And..Should I bother using size_t other places than when stdlib functions
need/return then in my library? (If yes, why?)

I bother because my code now works on linux,sun solaris and windoze (32
bits), and I don''t want strange problems running it on 64 bit
architectures.



如果你想表示

对象的大小,
size_t是最好用的类型。使用int来表示对象大小可能适用于大多数现代系统,但不能保证。另一方面,size_t未签名的

事实可能会造成一些陷阱。例如,

以下是无限循环:


size_t s;

for(s = 10; s> = 0; s - ){

/ * ... * /

}


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/ ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



size_t is the best type to use if you want to represent sizes of
objects. Using int to represent object sizes is likely to work on
most modern systems, but it isn''t guaranteed. On the other hand, the
fact that size_t is unsigned can create some pitfalls. For example,
the following is an infinite loop:

size_t s;
for (s = 10; s >= 0; s --) {
/* ... */
}

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


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

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