Memcpy vs Memmove
The C standard library (stdlib) facilitates two functions for copying memory regions, they are memcpy and memmove. We often wonder -
- Why memmove is there when memcpy is already there?
- Is memmove different from memcpy?
- When should we use memmove over memcpy?
Memcpy referance
NAME memcpy - copy area of memory bytes from source to destination. LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <string.h> void * memcpy(void * dst, const void * src, size_t n); DESCRIPTION The memcpy() function copies n bytes from memory from src location to memory pointed to dst. If dst and src area overlaps then behavior is undefined. Applications which might deal with dst and src might overlap should use memmove(3) instead. RETURN VALUE The memcpy() function returns the original value of dst.
Memcpy
memcpy() is generally used to copy a portion of memory chuck from one location to another location. memcpy() works fine when there is no overlapping between source and destination. Lets consider a overlapping of buffer in the front side/lower side. Below picture shows the details.
Memcpy implementation in C
However memcpy() works fine here. Now consider a case where overlapping is there at the trailing side or in upper side of the memory.
Here result is not expected. From the above graphical view it is clear that some mechanism should be there to take care of the overlapping and displacement of elements should be in the reverse direction when overlapping is at the trailing side. Below diagram shows how it should be.
Memmove referance
NAME memmove - copies bytes from source to destination while taking care of overlapping. LIBRARY Standard C Library (libc, -lc) SYNOPSIS #include <string.h> void * memmove(void *dst, const void *src, size_t n); DESCRIPTION The memmove() function copies n bytes from src area to dst area. These two areas might overlap; the copy process always done in a non-destructive manner. RETURN VALUES The memmove() function returns the original value of dst.
Memmove
Memmove copies memory blocks from source location to destination location like memcpy but it also takes care of the overlapping. Copy of the memory bytes are done in non-destructive manner.
Memmove implementation in C
About our authors: Team EQA
You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.
Further readings
Construct your own strcpy and strcat/implementation of strcpy, strcat function in C
strcpy, strcat implementation in c using pointers, strncpy, strcat implementation in c, strcmp, strcat implementation in c, strcat implementation in c, implementation of strcpy, strcat function in c
How strrev() works? Explain working principle with source code.
Explain implementation details of strrev. Explain working principle of strrev with source code. Shows how to swap characters with diagram
How to duplicate a string? (String duplication with strdup)
How to duplicate a string? (String duplication with strdup)
How to split/tokenize a string? (strtok() string tokenizer)
How to split/tokenize a string? (strtok() string tokenizer)