std::memset(3)
π std::memset β C++ Standard Libary, section 3 π
okay bestie, welcome to the std::memset page. it's a C++ Standard Libary function, it lives in section 3, and it is lowkey iconic for filling bytes. let's get into it π
β¨ NAME β¨
std::memsetβ bestie, the name is literallystd::memset. that's the assignment. π
π Synopsis π
Defined in header <cstring>
void* memset( void* dest, int ch, std::size_t count );
so here's the deal: it copies the value static_cast<unsigned char>(ch) into each of the first count characters of the object pointed to by dest.
π¨ undefined behavior alert, and I mean it: if the object is a potentially-overlapping subobject or is not TriviallyCopyable (e.g., scalar, C-compatible struct, or an array of trivially copyable type), the behavior is undefined. also, ifcountis greater than the size of the object pointed to bydest, the behavior is undefined. no cap, that's genuinely undefined behavior.
π Parameters π
destβ pointer to the object to fillchβ fill bytecountβ number of bytes to fill
π«Ά Return value π«Ά
it returns dest. that's it. that's the return value. periodt. β¨
π§ Notes π§
okay, this part is important, so listen up. 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). like, do not use it for that, bestie β it's giving insecure and also the compiler might just delete your whole attempt. π
Solutions for that include std::fill with volatile pointers, (C23) memset_explicit(), (C11) memset_s, FreeBSD explicit_bzero or Microsoft SecureZeroMemory. π«Ά
πΈ Example πΈ
here's the example, bestie:
// Run this code
#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';
}
π Output π
and the output is literally:
00000011000000110000001100000011
00000011000000110000001100000011
00000011000000110000001100000011
00000011000000110000001100000011
π See also π
memcpyβ copies one buffer to another (function)memmoveβ moves one buffer to another (function)wmemsetβ copies the given wide character to every position in a wide character array (function)fillβ copy-assigns the given value to every element in a range (function template)fill_nβ copy-assigns the given value to N elements in a range (function template)is_trivially_copyable(C++11) β checks if a type is trivially copyable (class template)- C documentation for
memsetβ the C lore, if you want it
π source: http://cppreference.com Β· version/date: 2024.06.10 Β· page: std::memset(3) Β· stay hydrated, memset responsibly, and remember: not me using memset to scrub passwords ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms