memmove vs memcpy

What is difference between memmove and memcpy (memmove vs memcpy)?

Generally, I saw many people asked the question that what is the difference between memcpy and memmove (memmove vs memcpy)?. So to solve the question I am writing an article on it but before going to compare them, I want to explain the implementation and working of memcpy and memmove.

What is memcpy in C?

The memcpy function copies n characters from the source object to the destination object. If copying takes place between objects that overlap, the behavior is undefined. You need to include <string.h> header file before using the memcpy function.

Syntax:

void *memcpy (void * restrict dst ,const void * src ,size_t n);


Parameters:

src — pointer to the source object

dst — pointer to the destination object

n — Number of bytes to copy.

 

How to implement own memcpy in C?

Generally, it is not recommended to use your own created memcpy because your compiler/standard library will likely have a very efficient and tailored implementation of memcpy.

But some times we need to create our own memcpy function due to some limitations. So let’s create our own memcpy function that copies n characters from source to the destination buffer.

You can create own memcpy using a simplest memory-transfer algorithm just reads one byte at a time and writes that byte before reading the next. You can call this algorithm byte-by-byte. This algorithm is easy to implement but may not offer optimal performance, particularly if your memory bus is wider than 8 bits.

So let’s see the code to implement your own memcpy,

void * my_memcpy(void* dest, const void* src, unsigned int n)
{
    char *pDest = (char *)dest;
    const char *pSrc =( const char*)src;

    // return if pDest and pSrc is NULL
    if ((pDest == NULL) &&(pSrc == NULL))
        return NULL;

    while(n) //till n
    {
        //Copy byte by byte
        *(pDest++) = *(pSrc++);
        --n;
    }

    return dest;
}

Code Analysis:

You can see the highlighted portion of the code. Line number 7 and 8 handle the scenario that source and destination memory must not be NULL.

In line 10 to 15, we have a while loop, the while loops copy character from source to destination one by one and incrementing the source and destination pointer by 1.

 

What is memmove in C?

The memmove function copies n characters from the source to the destination object. In memmove before copying the characters from source to destination object first copied the n character from source to the temporary array, after that copy n character from the temporary array to the destination object. It prevents from the undefined behavior when the source and destination object overlaps.

Syntax:

void *memmove(void * restrict dst, const void *src, size_t n);



Parameters:

src — pointer to the source object
dst — pointer to the destination object
n — Number of bytes to copy.

 

 

Note: If you want to learn C programming, then I want to recommend a good course from pluralsight which created by my one friend. Trial of this course is absolutely free 10 days including thousands of course.

Get your free trial

 

 

How to implement your own memmove in C?

Your compiler/standard library will likely have a very efficient and tailored implementation of memmove() function. So if not require avoid to create own version of the memmove function.

Implementation of the memmove is similar to memcpy but in memmove, we need to handle the overlapping scenario. So here I am creating a temporary array to handle the overlapping scenario.

Here we will copy all n characters in temp array first, then we will copy the temp array characters in destination buffer.

void * my_memmove(void* dest, const void* src, unsigned int n)
{
    char *pDest = (char *)dest;
    const char *pSrc =( const char*)src;

    //allocate memory for tmp array
    char *tmp  = (char *)malloc(sizeof(char ) * n);
    if(NULL == tmp)
    {
        return NULL;
    }
    else
    {
      unsigned int i = 0;

        // copy src to tmp array
        for(i =0; i < n ; ++i)
        {
            *(tmp + i) = *(pSrc + i);
        }
        //copy tmp to dest
        for(i =0 ; i < n ; ++i)
        {
            *(pDest + i) = *(tmp + i);
        }
        free(tmp); //free allocated memory
    }

    return dest;
}

 

What is the (memmove vs memcpy) difference between memcpy and memmove?

Now I think you are pretty familiar with memcpy and memmove functions. So its time to compare the memcpy and memmove function (memcpy vs memmove or memmove vs memcpy ).

  • The memcpy copy function shows undefined behavior if the memory regions pointed to by the source and destination pointers overlap. The memmove function has the defined behavior in case of overlapping. So whenever in doubt, it is safer to use memmove in place of memcpy.
#include <string.h>
#include <stdio.h>

char str1[50] = "I am going from Delhi to Gorakhpur";
char str2[50] = "I am going from Delhi to Gorakhpur";


int main()
{
    //Use of memmove
    printf( "Function:\tmemmove with overlap\n" );

    printf( "Orignal :\t%s\n",str1);

    printf( "Source:\t\t%s\n", str1 + 5 );

    printf( "Destination:\t%s\n", str1 + 11 );

    memmove( str1 + 11, str1 + 5, 29 );

    printf( "Result:\t\t%s\n", str1 );

    printf( "Length:\t\t%d characters\n\n", strlen( str1 ) );


    //Use of memcpy
    printf( "Function:\tmemcpy with overlap\n" );

    printf( "Orignal :\t%s\n",str2);

    printf( "Source:\t\t%s\n", str2 + 5 );

    printf( "Destination:\t%s\n", str2 + 11 );

    memcpy( str2 + 11, str2 + 5, 29 );

    printf( "Result:\t\t%s\n", str2 );

    printf( "Length:\t\t%d characters\n\n", strlen( str2 ) );


    return 0;
}

OutPut on the different platforms:

memcpy vs memmove

 

memmove vs memcpy

 

memcpy vs memmove

 

  • The memmove function is slower in comparison to memcpy because in memmove extra temporary array is used to copy n characters from the source and after that, it uses to copy the stored characters to the destination memory.
  • The memcpy is useful in forwarding copy but memmove is useful in case of overlapping scenarios.

Disadvantage of memmove and memcpy

  • Both memcpy and memmove does not check the terminating null character, so carefully using with strings.
  • The behavior of memcpy or memmove can be undefined if you try to access the destination and source buffer beyond their length.
  • memcpy or memmove does not check the validity of the destination buffer.
#include<stdio.h>
#include<string.h>

int main(void)
{

    char src[20] ="aticleworld.com";
    char* dst = NULL;

    memcpy(dst,src,sizeof(src));

    return 0;
}
  • memcpy or memmove does not check the validity of the source buffer.
#include<stdio.h>
#include<string.h>

int main(void)
{
    char *src = NULL;
    char dst[12] = {0};

    memcpy(dst,src,12);
    
    return 0;
}

 

Recommended Articles for you: