Home C++ Strings Functions memmove() C++ memmove() Function Definition In C++, memmove() copies bytes like memcpy, but it is safe when the source and destination overlap .
Include <cstring>.
This guide shows the C library helper in a C++ .cpp program with std::cout. Prefer std::string for new text code; keep these APIs for C interop and interviews.
Syntax Same arguments as memcpy.
std::memmove(dest, src, n); Call What it does std::memmove(dest, src, n)Copy n bytes; overlap allowed.
Slightly more work than memcpy in theory; use memcpy when you know there is no overlap.
Library declarations void* memmove(void* dest, const void* src, size_t n); Parameters Parameter Description dest, src Destination and source pointers. n Byte count.
Return Value Return Value Description void*Equals dest.
C++ Examples 1. Shift right 2. Non-overlap 3. Shift left Example 1 — Basic memmove() Overlap-safe shift inside one buffer.
#include <cstring>
#include <iostream>
int main() {
char s[] = "abc";
std::memmove(s + 1, s, 2);
s[0] = 'X';
std::cout << s << '\\n';
return 0;
} How It Works Bytes of ab move one place right. Overlap would break memcpy. Final string is Xab (with leftover). Example 2 — memmove Without Overlap Works like memcpy too.
#include <cstring>
#include <iostream>
int main() {
char a[] = "YZ";
char b[3] = {};
std::memmove(b, a, 3);
std::cout << b << '\\n';
return 0;
} How It Works Buffers are separate. Three bytes including null are moved. Print shows YZ. Example 3 — Overlap Left Shift Remove first character by shifting left.
#include <cstring>
#include <iostream>
int main() {
char s[5] = "abcd";
std::memmove(s, s + 1, 4);
std::cout << s << '\\n';
return 0;
} How It Works Characters from index 1 move to index 0. Null terminator moves too. Result is bcd. Frequently Asked Questions What does memmove do? Copies n bytes and handles overlapping regions.
When prefer memmove over memcpy? When source and destination might overlap.
Is memmove slower? It may be slightly; prefer memcpy when no overlap is guaranteed.
Does it know about strings? No—it copies raw bytes.
Next: memset() Keep going through the string functions. Fill a byte range with one value.
memset() tutorial → CODETOFUN NEWS
Stay Ahead in Tech Get the latest IT news, programming updates, and technology trends directly on your phone.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion