CKTIS / C
memset — filling memory with bytes
memset from <string.h> fills n bytes with c converted to unsigned char. It operates on bytes, not on the values of array elements.
Version: C23; repository compiler flag, production runtime not attested
Syntax
#include <string.h>
void *memset(void *s, int c, size_t n);memset(s, c, n) writes c, converted to unsigned char, into the first n bytes of the object pointed to by s. It returns s; <string.h> provides its declaration.
The region must be writable and large enough. Writing beyond its bounds causes undefined behavior. n counts bytes, not elements: memset(a, 1, sizeof a) is not a general way to set an int array's elements to 1.
In the example, sizeof data covers the entire unsigned char array, so every element receives 7.
Do not use ordinary memset for guaranteed secret erasure: the optimizer may eliminate an unobservable write. C23 provides memset_explicit for this purpose; selecting a C23 compiler flag alone does not establish its availability in the runtime library.
Examples
#include <stdio.h>
#include <string.h>
int main(void)
{
unsigned char data[4];
memset(data, 7, sizeof data);
printf("%u %u %u %u\n",
(unsigned int)data[0], (unsigned int)data[1],
(unsigned int)data[2], (unsigned int)data[3]);
return 0;
}
Result
7 7 7 7
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.