Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are function parameters of type size_t?

Tags:

c

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 is just an example, I want more general reasons. Thanks in advance.

like image 394
Grissiom Avatar asked Apr 08 '10 13:04

Grissiom


2 Answers

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.

like image 80
Judge Maygarden Avatar answered Sep 27 '22 02:09

Judge Maygarden


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.

like image 36
sharptooth Avatar answered Sep 23 '22 02:09

sharptooth