In C23, memset_explicit() stores a byte value throughout a memory block, like memset(). Its special purpose is to ensure that the writes are performed even when the compiler sees that the memory is never read again.
This makes it useful for clearing passwords, keys, and other sensitive bytes before storage is released or reused. It is a C23 library feature, so older C libraries may not declare or implement it.
Syntax
Start with this concrete call, then adjust the values for your buffer.
C
memset_explicit(secret, 0, sizeof secret);
Call
What it does
memset_explicit(secret, 0, n)
Writes zero into each of the n bytes and preserves the wipe.
Include <string.h> and compile against a C23 library that provides this function.
Library declaration
C
void *memset_explicit(void *s, int c, size_t n);
Parameters
Parameter
Description
s
Required. Pointer to the writable memory block.
c
Byte value to store. It is converted to unsigned char.
n
Number of bytes to overwrite.
Return Value
Return Value
Description
s
Returns the same pointer that was passed as the destination.
C Examples
Example 1 — Wipe a Password Buffer
Use the function after the program has finished checking a password.