C++ memmove()

The memmove() function in C++ copies a specified bytes of data from source to the destination.

memmove() prototype

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

The memmove() function takes three arguments: dest, src and count. When the memmove() function is called, it copies count bytes from the memory location pointed to by src to the memory location pointed to by dest.

Copying is performed even if the src and dest pointer overlaps. This is because copying takes place as if an intermediate buffer is created where the data are first copied to from src and then finally copied to dest.

It is defined in <cstring> header file.

memmove() Parameters

  • dest: Pointer to the memory location where the contents are copied to
  • src: Pointer to the memory location where the contents are copied from.
  • count: Number of bytes to copy from src to dest.

memmove() Return value

The memmove() function returns dest, the pointer to the destination memory location.

Example: How memmove() function works

#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    int arr[10] = {8,3,11,61,-22,7,-6,2,13,47};
    int *new_arr = &arr[5];

    memmove(new_arr,arr,sizeof(int)*5);

    cout << "After copying" << endl;
    for (int i=0; i<10; i++)
        cout << arr[i] << endl;
    return 0;

}

When you run the program, the output will be:

After copying 8 3 11 61 -22 8 3 11 61 -22