std::memset(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸŽ€ 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 literally std::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, if count is greater than the size of the object pointed to by dest, the behavior is undefined. no cap, that's genuinely undefined behavior.

πŸ’– Parameters πŸ’–

  • dest β€” pointer to the object to fill
  • ch β€” fill byte
  • count β€” 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