C++ memmove() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
<cstring>

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.

C++
std::memmove(dest, src, n);
CallWhat 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
C++
void* memmove(void* dest, const void* src, size_t n);

Parameters

ParameterDescription
dest, srcDestination and source pointers.
nByte count.

Return Value

Return ValueDescription
void*Equals dest.

C++ Examples

Example 1 — Basic memmove()

Overlap-safe shift inside one buffer.

C++
#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

  1. Bytes of ab move one place right.
  2. Overlap would break memcpy.
  3. Final string is Xab (with leftover).

Example 2 — memmove Without Overlap

Works like memcpy too.

C++
#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

  1. Buffers are separate.
  2. Three bytes including null are moved.
  3. Print shows YZ.

Example 3 — Overlap Left Shift

Remove first character by shifting left.

C++
#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

  1. Characters from index 1 move to index 0.
  2. Null terminator moves too.
  3. Result is bcd.

Frequently Asked Questions

Copies n bytes and handles overlapping regions.
When source and destination might overlap.
<cstring>.
It may be slightly; prefer memcpy when no overlap is guaranteed.
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.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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