开发者

Why are function parameters of type size_t?

开发者 https://www.devze.com 2022-12-25 21:17 出处:网络
The prototype of memset is void *memset(void *s, int c, size_t n);. So why the third parameter is of type size_t ? memset i开发者_如何转开发s just an example, I want more general reasons. Thanks in ad

The prototype of memset is void *memset(void *s, int c, size_t n);. So why the third parameter is of type size_t ? memset i开发者_如何转开发s just an example, I want more general reasons. Thanks in advance.


size_t is the return type of the sizeof operator and is used to describe memory sizes. In the case of memset, it specifies the number of bytes (n) in the memory block (s) that should be set to the given value (c).

The size in bits of size_t varies based on the address space of the target platform. It does not always correlate to the register size. For example, in a segmented memory architecture the sizeof (size_t) can be smaller than the sizeof (void *). Typically, size_t would be 4 bytes on a 32-bit machine, 8 bytes on a 64-bit machine, etc.


size_t is the type used to denote the size of objects. In C sizes of integer types (int, long, etc.) are implementation-dependent and you need to use the right type on each compiler implementationp so that the size is big enough to store all possible values.

The headers that will come with the platform SDK will have a typedef that will map size_t to the right integer type. So you write memset() once and it compiles right on every implementation.


size_t is a type suitable for representing the amount of memory a data object requires. It is an unsigned integer type (generally, typedef unsigned int size_t;).

Read this link for more information.


size_t is guaranteed to be large enough to hold a pointer on all platforms.

0

精彩评论

暂无评论...
验证码 换一张
取 消