Namespaces
Variants
Views
Actions

std::memcpy

From cppreference.com
< cpp‎ | string‎ | byte
Revision as of 23:25, 31 May 2013 by P12bot (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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.

If the objects overlap, the behavior is undefined. If the objects are not trivially copyable (e.g. scalars, arrays, C-compatible structs), 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

dest

[edit] Example

#include <iostream>
#include <cstring>
 
int main()
{
    char source[] = "once upon a midnight dreary...";
    char dest[4];
 
    std::memcpy(dest, source, sizeof dest);
 
    for (char c : dest) {
        std::cout << c << '\n';
    }
}

Output:

o
n
c
e

[edit] See also

moves one buffer to another
(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