C Language

What is the difference between memmove and memcpy?

What is the difference between memmove and memcpy

Generally, I saw many people asked the question that what is the difference between memcpy and memmove? 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 the source and destination objects overlap, the behavior of memcpy is undefined.

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?

Implementation of memcpy is not a big deal, you need to typecast the given source and destination address to char* (1 byte). After the typecasting copy the data from the source to destination one by one till n (given length).

Let see an example code that describes implementation of memcpy,

What is memmove in C?

The memmove function copies n characters from the source to the destination object. In memmove before copying the characters 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 that is occurred in the overlapping scenario.

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.



How to implement own memmove in C?

Implementation of the memmove is similar to memcpy but in memmove need to take an extra array of size n. Before copying the characters need to typecast the address of source and destination with char *. After the typecasting copying n character from source to the temporary array. When copy has been completed than copy the n character from temporary array to the destination array.

Let see an example code that describes implementation of memmove,

What is the difference between memcpy and memmove?

Both copies function are used to copy n characters from the source object to destination object but they have some difference that is mentioned below.

  • 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.

OutPut:

 

Function: memmove with overlap
Orignal : I am going from Delhi to Gorakhpur
Source: going from Delhi to Gorakhpur
Destination: from Delhi to Gorakhpur
Result: I am going going from Delhi to Gorakhpur
Length: 40 characters

Function: memcpy with overlap
Orignal : I am going from Gorakhpur to Delhi
Source: going from Gorakhpur to Delhi
Destination: from Gorakhpur to Delhi
Result: I am going going fring frakg frako frako
Length: 40 characters

  • 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 scenario.




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.

  • memcpy or memmove does not check the validity of the source buffer.




2 Comments

  1. memmove does not use extra buffer, that would be unnecessary slow (also because of malloc). Instead memmove is just using incrementing or decrementing order of “bytes”, depending on source address is higher or lower then destination address, respectively.
    memmove is only slightly slower then memcpy, just because of determining the order and moving pointers to the end of buffers in the second case.

    • Amlendra

      There are a lot of ways to implement the memmove function. The C committee mentioned that(in article 7.24.2.2,) the memmove use a temporary array before copies the n characters from the source to the destination buffer. You can see the mentioned link that describes the different ways to implement the memmove function: https://aticleworld.com/memmove-function-implementation-in-c/

Leave a Reply

Theme by Anders Norén