Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::memcpy — copying memory bytes

std::memcpy from <cstring> copies a specified number of bytes between non-overlapping memory regions. It does not check bounds and is not a replacement for copying resource-owning objects.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

#include <cstring>
void* std::memcpy(void* dest, const void* src, std::size_t count);

std::memcpy(dest, src, count) from <cstring> copies count bytes from src to dest, not count elements. It does not stop at a zero byte. It returns a pointer to a suitable implicitly created object or, if there is none, the value of dest.

The source must provide the required number of readable bytes, and the destination must provide writable bytes. The regions must not overlap: violating this requirement causes undefined behavior; use std::memmove for overlapping regions. The function does not check buffer capacities.

Copying the complete representation between distinct objects of the same trivially copyable type preserves the value, provided neither is a potentially-overlapping subobject. Do not treat this operation as a replacement for copy constructors or assignment of resource-owning objects. Implicit object creation does not remove lifetime and type restrictions.

In the example, sizeof src covers the entire array, not a pointer's size. Both arrays contain int and occupy separate memory regions. The expected output is 10 20 30 followed by a newline.

Examples

#include <cstring>
#include <iostream>

int main() {
    const int src[] = {10, 20, 30};
    int dest[3] = {};

    std::memcpy(dest, src, sizeof src);

    std::cout << dest[0] << ' ' << dest[1] << ' ' << dest[2] << '\n';
}

Result

10 20 30

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources