Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::memcpy

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

      Performs the following operations in order:

      1. Implicitly creates objects atdest.
      2. Copiescount characters (as if of typeunsignedchar) from the object pointed to bysrc into the object pointed to bydest.

      If any of the following conditions is satisfied, the behavior is undefined:

      Contents

      [edit]Parameters

      dest - pointer to the memory location to copy to
      src - pointer to the memory location to copy from
      count - number of bytes to copy

      [edit]Return value

      If there is asuitable created object, returns a pointer to it; otherwise returnsdest.

      [edit]Notes

      std::memcpy is meant to be the fastest library routine for memory-to-memory copy. It is usually more efficient thanstd::strcpy, which must scan the data it copies orstd::memmove, which must take precautions to handle overlapping inputs.

      Several C++ compilers transform suitable memory-copying loops tostd::memcpy calls.

      Wherestrict aliasing prohibits examining the same memory as values of two different types,std::memcpy may be used to convert the values.

      [edit]Example

      Run this code
      #include <cstdint>#include <cstring>#include <iostream> int main(){// simple usagechar source[]="once upon a daydream...", dest[4];    std::memcpy(dest, source, sizeof dest);std::cout<<"dest[4] = {";for(int n{};char c: dest)std::cout<<(n++?", ":"")<<'\''<< c<<"'";std::cout<<"};\n"; // reinterpretingdouble d=0.1;//  std::int64_t n = *reinterpret_cast<std::int64_t*>(&d); // aliasing violationstd::int64_t n;    std::memcpy(&n,&d, sizeof d);// OK std::cout<<std::hexfloat<< d<<" is "<<std::hex<< n<<" as a std::int64_t\n"<<std::dec; // object creation in destination bufferstruct S{int x{42};void print()const{std::cout<<'{'<< x<<"}\n";}} s;    alignas(S)char buf[sizeof(S)];    S* ps= new(buf) S;// placement new    std::memcpy(ps,&s, sizeof s);    ps->print();}

      Output:

      dest[4] = {'o', 'n', 'c', 'e'};0x1.999999999999ap-4 is 3fb999999999999a as a std::int64_t{42}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 4064C++98it was unclear whether the returned pointer points to a suitable created objectmade clear

      [edit]See also

      moves one buffer to another
      (function)[edit]
      fills a buffer with a character
      (function)[edit]
      copies a certain amount of wide characters between two non-overlapping arrays
      (function)[edit]
      copies characters
      (public member function ofstd::basic_string<CharT,Traits,Allocator>)[edit]
      copies a range of elements to a new location
      (function template)[edit]
      copies a range of elements in backwards order
      (function template)[edit]
      checks if a type is trivially copyable
      (class template)[edit]
      C documentation formemcpy
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/string/byte/memcpy&oldid=180355"

      [8]ページ先頭

      ©2009-2025 Movatter.jp