std::memset
Header: <cstring>
Copies the value static_cast
# Declarations
void* memset( void* dest, int ch, std::size_t count );
# Parameters
dest: pointer to the object to fillch: fill bytecount: number of bytes to fill
# Return value
dest
# Notes
std::memset may be optimized away (under the as-if rules) if the object modified by this function is not accessed again for the rest of its lifetime (e.g., gcc bug 8537). For that reason, this function cannot be used to scrub memory (e.g., to fill an array that stored a password with zeroes).
Solutions for that include std::fill with volatile pointers, (C23) memset_explicit(), (C11) memset_s, FreeBSD explicit_bzero or Microsoft SecureZeroMemory.
# Example
#include <bitset>
#include <climits>
#include <cstring>
#include <iostream>
int main()
{
int a[4];
using bits = std::bitset<sizeof(int) * CHAR_BIT>;
std::memset(a, 0b1111'0000'0011, sizeof a);
for (int ai : a)
std::cout << bits(ai) << '\n';
}