Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::memset

      From cppreference.com
      <cpp‎ |string‎ |byte
       
       
       
       
      Defined in header<cstring>
      void* memset(void* dest,int ch,std::size_t count);

      Copies the valuestatic_cast<unsignedchar>(ch) into each of the firstcount characters of the object pointed to bydest. If the object is apotentially-overlapping subobject or is notTriviallyCopyable (e.g., scalar, C-compatible struct, or an array of trivially copyable type), the behavior is undefined. Ifcount is greater than the size of the object pointed to bydest, the behavior is undefined.

      Contents

      [edit]Parameters

      dest - pointer to the object to fill
      ch - fill byte
      count - number of bytes to fill

      [edit]Return value

      dest

      [edit]Notes

      std::memset may be optimized away (under theas-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 includestd::fill with volatile pointers,(C23)memset_explicit(),(C11)memset_s, FreeBSDexplicit_bzero or MicrosoftSecureZeroMemory.

      [edit]Example

      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:

      00000011000000110000001100000011000000110000001100000011000000110000001100000011000000110000001100000011000000110000001100000011

      [edit]See also

      copies one buffer to another
      (function)[edit]
      moves one buffer to another
      (function)[edit]
      copies the given wide character to every position in a wide character array
      (function)[edit]
      copy-assigns the given value to every element in a range
      (function template)[edit]
      copy-assigns the given value to N elements in a range
      (function template)[edit]
      checks if a type is trivially copyable
      (class template)[edit]
      C documentation formemset
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/byte/memset&oldid=152827"

      [8]ページ先頭

      ©2009-2025 Movatter.jp