std::memset (3) - Linux Manuals

std::memset: std::memset

NAME

std::memset - std::memset

Synopsis


Defined in header <cstring>
void* memset( void* dest, int ch, std::size_t count );


Converts the value ch to unsigned char and copies it into each of the first count characters of the object pointed to by dest. 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. If count is greater than the size of the object pointed to by dest, the behavior is undefined.

Parameters


dest - pointer to the object to fill
ch - fill byte
count - 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, C11 memset_s, FreeBSD explicit_bzero or Microsoft SecureZeroMemory.

Example


// Run this code


  #include <iostream>
  #include <cstring>


  int main()
  {
      int a[20];
      std::memset(a, 0, sizeof a);
      for (int ai : a) std::cout << ai;
  }

Output:


  00000000000000000000

See also


                      copies one buffer to another
memcpy (function)
                      moves one buffer to another
memmove (function)
                      copies the given wide character to every position in a wide character array
wmemset (function)
                      copy-assigns the given value to every element in a range
fill (function template)
                      copy-assigns the given value to N elements in a range
fill_n (function template)


is_trivially_copyable checks if a type is trivially copyable
                      (class template)
(C++11)