何时/为何使用size_t [英] When/why to use size_t

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

问题描述

为什么编译器中定义的size_t类型除了unsigned

int / long?

何时/为什么要使用size_t?

Alex Vinokur

电子邮件:alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Why was the size_t type defined in compilers in addition to unsigned
int/long?
When/why should one use size_t?
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

推荐答案

Alex Vinokur写道:
Alex Vinokur wrote:
为什么编译器中定义的size_t类型除了unsigned
int / long?
何时/为什么要使用size_t?
Why was the size_t type defined in compilers in addition to unsigned
int/long?
When/why should one use size_t?




size_t被定义为一个类型,用于保存C中对象的大小。

能够表示最大可能对象的大小

由C实现支持。 sizeof运算符产生的值为

此类型。您可以使用size_t来保存您声明的对象的大小

并在程序中进行操作。与使用无符号或无符号长整数相比,它更便于携带,而且b $ b更清晰。但是不同的程序员在这方面有自己的偏好。



size_t was defined as a type to hold the size of an object in C. It is
capable of representing the size of the largest possible object
supported by a C implementation. The sizeof operator yields a value of
this type. You can use size_t to hold the size of objects you declare
and manipulate in your programs. It''s slightly more portable and
clearer than using unsigned or unsigned long. But different programmers
have their own preferences in this regard.


Alex Vinokur写道:
Alex Vinokur wrote:

为什么编译器中定义的size_t类型除了unsigned
int / long?


如果unsigned足够大,可以完成这项工作

,也是比未签名的更快的类型,

size_t应该是未签名的。

如果unsigned不够大,

那么size_t应该是长签名的(C89)。

何时/为什么要使用size_t?

Why was the size_t type defined in compilers in addition to unsigned
int/long?
In the case where unsigned is big enough to do the job
and also a faster type than long unsigned,
size_t should be unsigned.
In a case where unsigned isn''t big enough,
then size_t should be long unsigned, (C89).
When/why should one use size_t?




计算数组的字节或元素时,

或与返回值接口时

of sizeof运算符或返回size_t的函数。

我喜欢在ac程序中匹配数据类型

只要不太困难。


-

pete



When counting bytes or elements of an array,
or when interfacing with the return values
of the sizeof operator or functions that return size_t.
I like to match data types in a c program
whenever it''s not too difficult.

--
pete


Alex Vinokur说:
Alex Vinokur said:
除了unsigned
int / long之外,为什么编译器中定义了size_t类型?


因为size_t与unsigned int和unsigned long的目的不同。

它的主要目的是能够包含一个等于<的大小的值br />
任何对象,虽然它当然可以用于其他东西。

何时/为什么要使用size_t?
Why was the size_t type defined in compilers in addition to unsigned
int/long?
Because size_t has a different purpose to unsigned int and unsigned long.
Its primary purpose is to be able to contain a value equal to the size of
any object, although it can certainly be used for other things.
When/why should one use size_t?




当存储对象的大小或对象的数量时。要说明这一点,只需看看一些标准库函数

使用size_t:


int setvbuf(FILE * stream,char * buf,int mode,size_t size);

size_t fread(void * ptr,size_t size,size_t nmemb,

FILE * stream);

size_t fwrite(const void * ptr,size_t size,size_t nmemb,

FILE * stream);

void * calloc(size_t nmemb,size_t size) ;

void * malloc(size_t size);

void * realloc(void * ptr,size_t size);

void * bsearch(const void * key,const void * base,

size_t nmemb,size_t size,

int(* compar)(const void *,const void *));

void qsort(void * base,size_t nmemb,size_t size,

int(* compar)(const void *,const void *));

int mblen(const char * s,size_t n);

int mbtowc(wchar_t * pwc,const char * s,size_t n);

size_t mbstowcs(wchar_t * pwcs ,const char * s,size_t n);

size_t wcstombs(char * s,const wchar_t * pwcs,size_t n);

void * memcpy(void * s1,const void * s2,size_t n);

void * memmove(void * s1,const void * s2,size_t n);

char * strncpy(char * s1,const char * s2,size_t n);

char * strncat(char * s1,const char * s2,size_t n);

int memcmp(const void * s1,const void * s2,size_t n);

int strncmp(const char * s1,const char * s2,size_t n);

size_t strxfrm(char * s1,const char * s2,size_t n);

void * memchr(const void * s,int c,size_t n);

size_t strcspn(const char * s1,const char * s2);

size_t strspn(const char * s1,const char * s2);

void * memset(void * s,int c,size_t n);

size_t strlen(const char * s);

size_t strftime (char * s,size_t maxsize,

const char * format,const struct tm * timeptr);

-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上面的域名(但显然放弃了www)



When one is storing either the size of an object, or a count of objects. To
illustrate this, just look at some of the standard library functions that
use size_t:

int setvbuf(FILE *stream, char *buf, int mode, size_t size);
size_t fread(void *ptr, size_t size, size_t nmemb,
FILE *stream);
size_t fwrite(const void *ptr, size_t size, size_t nmemb,
FILE *stream);
void *calloc(size_t nmemb, size_t size);
void *malloc(size_t size);
void *realloc(void *ptr, size_t size);
void *bsearch(const void *key, const void *base,
size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
void qsort(void *base, size_t nmemb, size_t size,
int (*compar)(const void *, const void *));
int mblen(const char *s, size_t n);
int mbtowc(wchar_t *pwc, const char *s, size_t n);
size_t mbstowcs(wchar_t *pwcs, const char *s, size_t n);
size_t wcstombs(char *s, const wchar_t *pwcs, size_t n);
void *memcpy(void *s1, const void *s2, size_t n);
void *memmove(void *s1, const void *s2, size_t n);
char *strncpy(char *s1, const char *s2, size_t n);
char *strncat(char *s1, const char *s2, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
int strncmp(const char *s1, const char *s2, size_t n);
size_t strxfrm(char *s1, const char *s2, size_t n);
void *memchr(const void *s, int c, size_t n);
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
void *memset(void *s, int c, size_t n);
size_t strlen(const char *s);
size_t strftime(char *s, size_t maxsize,
const char *format, const struct tm *timeptr);
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


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

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