memmove() function in C++

In this guide, you will learn what is memmove() function is in C++ programming and how to use it with an example.

1. memmove() Function Overview

The memmove() function in C++ is utilized to copy memory areas. Unlike memcpy(), memmove() is designed to handle overlapping memory regions safely. It is an integral function found in the C++ <cstring> header, often used when copying blocks of memory without causing unintended alterations, especially in cases where source and destination memory regions overlap.


void* memmove(void* dest, const void* src, size_t n);


- dest: Pointer to the destination memory location.

- src: Pointer to the source memory location.

- n: Number of bytes to copy.

2. Source Code Example

#include <iostream>
#include <cstring>

int main() {
    char str[] = "C++Programming";

    // Creating overlap by moving the "C++" part further into the string
    memmove(str + 4, str, strlen(str) + 1);

    std::cout << "Modified string after memmove: " << str << std::endl;

    return 0;


Modified string after memmove: C++C++Programming

3. Explanation

In the provided source code:

1. We begin by including the essential header files: <iostream> for input/output procedures and <cstring> for memmove() and strlen().

2. We define a character array named str.

3. The memmove() function is employed to shift the initial segment of the string ("C++") further down, thus creating an overlap.

4. The size parameter for memmove() is determined using strlen(str) + 1 to make sure the null terminator is also shifted.

5. The modified string is then printed to demonstrate the successful movement of memory regions.