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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With