Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/memcpy"

From cppreference.com
< cpp‎ | string‎ | byte
(Broaden UB condition from nullptr to any invalid pointer. See http://lists.llvm.org/pipermail/llvm-dev/2017-July/115672.html)
m (Notes: | → #)
 
(11 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
{{cpp/title|memcpy}}
 
{{cpp/title|memcpy}}
 
{{cpp/string/byte/navbar}}
 
{{cpp/string/byte/navbar}}
{{ddcl | header=cstring |
+
{{ddcl|header=cstring|
 
void* memcpy( void* dest, const void* src, std::size_t count );
 
void* memcpy( void* dest, const void* src, std::size_t count );
 
}}
 
}}
  
Copies {{tt|count}} bytes from the object pointed to by {{tt|src}} to the object pointed to by {{tt|dest}}. Both objects are reinterpreted as arrays of {{c|unsigned char}}.
+
Copies {{c|count}} bytes from the object pointed to by {{c|src}} to the object pointed to by {{c|dest}}. Both objects are reinterpreted as arrays of {{c|unsigned char}}.
  
 
If the objects overlap, the behavior is undefined.
 
If the objects overlap, the behavior is undefined.
  
If either {{tt|dest}} or {{tt|src}} is an invalid pointer, the behavior is undefined, even if {{tt|count}} is zero.
+
If either {{c|dest}} or {{c|src}} is an [[cpp/language/pointer#Pointers|invalid or null pointer]], the behavior is undefined, even if {{c|count}} is zero.
  
If the objects are [[cpp/language/object#Subobjects|potentially-overlapping]] or not {{named req|TriviallyCopyable}}, the behavior of {{tt|memcpy}} is not specified and [http://stackoverflow.com/questions/29777492 may be undefined].
+
If the objects are [[cpp/language/object#Subobjects|potentially-overlapping]] or not {{named req|TriviallyCopyable}}, the behavior of {{tt|memcpy}} is not specified and [https://stackoverflow.com/questions/29777492 may be undefined].
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | dest | pointer to the memory location to copy to}}
+
{{par|dest|pointer to the memory location to copy to}}
{{par | src | pointer to the memory location to copy from}}
+
{{par|src|pointer to the memory location to copy from}}
{{par | count | number of bytes to copy}}
+
{{par|count|number of bytes to copy}}
 
{{par end}}
 
{{par end}}
  
 
===Return value===
 
===Return value===
{{tt|dest}}
+
{{c|dest}}
  
 
===Notes===
 
===Notes===
 +
{{tt|std::memcpy}} may be used to [[cpp/language/object#Object creation|implicitly create]] objects in the destination buffer.
 +
 
{{tt|std::memcpy}} is meant to be the fastest library routine for memory-to-memory copy. It is usually more efficient than {{lc|std::strcpy}}, which must scan the data it copies or {{lc|std::memmove}}, which must take precautions to handle overlapping inputs.
 
{{tt|std::memcpy}} is meant to be the fastest library routine for memory-to-memory copy. It is usually more efficient than {{lc|std::strcpy}}, which must scan the data it copies or {{lc|std::memmove}}, which must take precautions to handle overlapping inputs.
  
 
Several C++ compilers transform suitable memory-copying loops to {{tt|std::memcpy}} calls.
 
Several C++ compilers transform suitable memory-copying loops to {{tt|std::memcpy}} calls.
  
Where [[cpp/language/object#Strict_aliasing|strict aliasing]] prohibits examining the same memory as values of two different types, {{tt|std::memcpy}} may be used to convert the values.
+
Where {{lsd|cpp/language/object#Strict aliasing}} prohibits examining the same memory as values of two different types, {{tt|std::memcpy}} may be used to convert the values.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <iostream>
+
 
#include <cstdint>
 
#include <cstdint>
 
#include <cstring>
 
#include <cstring>
 +
#include <iostream>
  
 
int main()
 
int main()
 
{
 
{
 
     // simple usage
 
     // simple usage
     char source[] = "once upon a midnight dreary...", dest[4];
+
     char source[] = "once upon a daydream...", dest[4];
 
     std::memcpy(dest, source, sizeof dest);
 
     std::memcpy(dest, source, sizeof dest);
     for (char c : dest)
+
    std::cout << "dest[4] = {";
         std::cout << c << '\n';
+
     for (int n{}; char c : dest)
 +
         std::cout << (n++ ? ", " : "") << '\'' << c << "'";
 +
    std::cout << "};\n";
  
 
     // reinterpreting
 
     // reinterpreting
Line 53: Line 56:
  
 
     std::cout << std::hexfloat << d << " is " << std::hex << n
 
     std::cout << std::hexfloat << d << " is " << std::hex << n
               << " as an std::int64_t\n";
+
               << " as a std::int64_t\n" << std::dec;
 +
 
 +
    // object creation in destination buffer
 +
    struct 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=
+
|output=
o
+
dest[4] = {'o', 'n', 'c', 'e'};
n
+
0x1.999999999999ap-4 is 3fb999999999999a as a std::int64_t
c
+
{42}
e
+
0x1.999999999999ap-4 is 3fb999999999999a as an std::int64_t
+
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/string/byte/dsc memmove}}
+
{{dsc inc|cpp/string/byte/dsc memmove}}
{{dsc inc | cpp/string/byte/dsc memset}}
+
{{dsc inc|cpp/string/byte/dsc memset}}
{{dsc inc | cpp/string/wide/dsc wmemcpy}}
+
{{dsc inc|cpp/string/wide/dsc wmemcpy}}
{{dsc inc | cpp/algorithm/dsc copy}}
+
{{dsc inc|cpp/string/basic_string/dsc copy}}
{{dsc inc | cpp/algorithm/dsc copy_backward}}
+
{{dsc inc|cpp/algorithm/dsc copy}}
{{dsc inc | cpp/types/dsc is_trivially_copyable}}
+
{{dsc inc|cpp/algorithm/dsc copy_backward}}
{{dsc see c | c/string/byte/memcpy}}
+
{{dsc inc|cpp/types/dsc is_trivially_copyable}}
 +
{{dsc see c|c/string/byte/memcpy}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
 
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 08:01, 25 October 2023

Defined in header <cstring>
void* memcpy( void* dest, const void* src, std::size_t count );

Copies count bytes from the object pointed to by src to the object pointed to by dest. Both objects are reinterpreted as arrays of unsigned char.

If the objects overlap, the behavior is undefined.

If either dest or src is an invalid or null pointer, the behavior is undefined, even if count is zero.

If the objects are potentially-overlapping or not TriviallyCopyable, the behavior of memcpy is not specified and may be 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

dest

[edit] Notes

std::memcpy may be used to implicitly create objects in the destination buffer.

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

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

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

[edit] Example

#include <cstdint>
#include <cstring>
#include <iostream>
 
int main()
{
    // simple usage
    char 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";
 
    // reinterpreting
    double d = 0.1;
//  std::int64_t n = *reinterpret_cast<std::int64_t*>(&d); // aliasing violation
    std::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 buffer
    struct 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] 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 of std::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 for memcpy