Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | string‎ | byte
(rephrase in terms of the object lifetime rules)
(let's just link to the SO)
Line 9: Line 9:
 
If the objects overlap, the behavior is undefined.
 
If the objects overlap, the behavior is undefined.
  
If the objects are not {{concept|TriviallyCopyable}} (e.g. scalars, arrays, C-compatible structs), {{tt|memcpy}} ends the [[cpp/language/lifetime|lifetime]] of the target object without running its destructor or creating a new object in its place, and the program has undefined behavior if
+
If the objects are not {{concept|TriviallyCopyable}}, the behavior of {{tt|memcpy}} is not specified and [http://stackoverflow.com/questions/29777492 may be undefined].
* it depends on the effects of the destructor of the target object; or
+
* the target object does not have dynamic [[cpp/language/storage duration|storage duration]], and the program does not ensure that a new object of the same type is constructed at the location of the target object (e.g., by placement-new) before the implicit destructor call takes place; or
+
* the program violates any of the [[cpp/language/lifetime#Access outside of lifetime|rules on accessing an object outside its lifetime]].
+
  
 
===Parameters===
 
===Parameters===

Revision as of 17:46, 28 April 2015

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 the objects are not Template:concept, the behavior of memcpy is not specified and may be undefined.

Contents

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

Return value

dest

Notes

std::memcpy is 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.

Example

#include <iostream>
#include <cstdint>
#include <cstring>
 
int main()
{
    // simple usage
    char source[] = "once upon a midnight dreary...", dest[4];
    std::memcpy(dest, source, sizeof dest);
    for (char c : dest)
        std::cout << c << '\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 an std::int64_t\n";
}

Output:

o
n
c
e
0x1.999999999999ap-4 is 3fb999999999999a as an std::int64_t

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 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