CKTIS / C++
std::memcpy and trivially copyable object representations
Learn when copying bytes preserves an object's value in C++23, and why that guarantee does not make byte copying a substitute for copying arbitrary objects.
Version: ISO C++23; implementation support may vary
Syntax
#include <cstring>
// Call: destination first; count is in bytes.
std::memcpy(dest, src, count);The key distinction behind std::memcpy and trivially copyable object representations is that copying bytes is not the same as copying an object by its type's rules. std::memcpy(dest, src, count) from <cstring> copies exactly count bytes from source to destination. Both pointers must be valid and non-null, even when count == 0. The source range must be readable and the destination writable. An out-of-bounds copy or overlapping ranges cause undefined behavior, not an error report. Use std::memmove for overlap; the other requirements still apply.
A complete object of type T has an object representation of sizeof(T) bytes. The bits that encode the value make up the value representation; the rest are padding bits. For a complete object type T, check trivial copyability with std::is_trivially_copyable_v<T> from <type_traits>. This trait does not check pointers or ranges.
Copy all sizeof(T) bytes from an initialized object to a distinct, already-live writable object of the same trivially copyable type T, and the destination acquires the source's value. Neither object may be a potentially overlapping subobject: a base-class subobject or a non-static data member marked [[no_unique_address]]. You can also save such an object's representation in a char, unsigned char, or std::byte array and restore the unchanged bytes to that same object.
In C++23, the operation can implicitly start the lifetimes of objects of implicit-lifetime types in suitably aligned destination storage before copying, if doing so gives defined behavior. It does not run constructors or start the lifetimes of subobjects that are not themselves of implicit-lifetime types. The void* result points to a suitable created object if there is one; otherwise it is dest.
Copy a std::string by construction or assignment, not by copying its bytes. Copying a pointer value does not copy what it points to. Padding can make std::memcmp distinguish equal values, and implementation-dependent layout and byte order make raw representations unsuitable as a portable format. Decode and validate untrusted data according to a defined format rather than treating arbitrary bytes as a valid T.
Examples
#include <cstddef>
#include <cstring>
#include <iostream>
#include <type_traits>
struct Record {
int id;
int score;
};
static_assert(std::is_trivially_copyable_v<Record>);
int main() {
Record source{7, 42};
Record target{};
std::memcpy(&target, &source, sizeof(Record));
std::cout << "copy: " << target.id << ' ' << target.score << '\n';
std::byte saved[sizeof(Record)];
std::memcpy(saved, &source, sizeof(Record));
source = Record{0, 0};
std::memcpy(&source, saved, sizeof(Record));
std::cout << "restored: " << source.id << ' ' << source.score << '\n';
}Result
copy: 7 42 restored: 7 42
`static_assert` checks the `Record` type. The program copies the value into `target`, then restores the original value of `source` from `saved`. It prints member values, not raw bytes.
Related lessons
- C++ · Lesson 16 · Advanced · Version: 3