Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

memmove() in C/C++

  • Difficulty Level : Medium
  • Last Updated : 28 Jun, 2021

memmove() is used to copy a block of memory from a location to another. It is declared in string.h

// Copies "numBytes" bytes from address "from" to address "to"
void * memmove(void *to, const void *from, size_t numBytes);

Below is a sample C program to show the working of memmove().

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

C




/* A C program to demonstrate working of memmove */
#include <stdio.h>
#include <string.h>
 
int main()
{
    char str1[] = "Geeks"; // Array of size 100
    char str2[] = "Quiz"; // Array of size 5
 
    puts("str1 before memmove ");
    puts(str1);
 
    /* Copies contents of str2 to sr1 */
    memmove(str1, str2, sizeof(str2));
 
    puts("\nstr1 after memmove ");
    puts(str1);
 
    return 0;
}
Output
str1 before memmove 
Geeks

str1 after memmove 
Quiz

How is it different from memcpy()? 



memcpy() simply copies data one by one from one location to another. On the other hand memmove() copies the data first to an intermediate buffer, then from the buffer to destination.
memcpy() leads to problems when strings overlap. 

For example, consider below program. 

C




// Sample program to show that memcpy() can loose data.
#include <stdio.h>
#include <string.h>
int main()
{
    char csrc[100] = "Geeksfor";
    memcpy(csrc + 5, csrc, strlen(csrc) + 1);
    printf("%s", csrc);
    return 0;
}
Output
GeeksGeeksfor

Since the input addresses are overlapping, the above program overwrites the original string and causes data loss. 

Consider the below program for understanding the difference between the memcpy and memmove function in case of overlapping happens.

C




// Sample program to show that memmove() is better than
// memcpy() when addresses overlap.
#include <stdio.h>
#include <string.h>
int main()
{
    char str[100] = "Learningisfun";
    char *first, *second;
    first = str;
    second = str;
    printf("Original string :%s\n ", str);
     
    // when overlap happens then it just ignore it
    memcpy(first + 8, first, 10);
    printf("memcpy overlap : %s\n ", str);
 
    // when overlap it start from first position
    memmove(second + 8, first, 10);
    printf("memmove overlap : %s\n ", str);
 
    return 0;
}
Output
Original string :Learningisfun
 memcpy overlap : LearningLearningis
 memmove overlap : LearningLearningLe
 

As you can see clearly with memmove function whenever overlap happens (i.e when the first pointer moves to the character ‘i’) then the first pointer will start to print from the beginning (output Le) but with memcpy function, it just ignores if there is an overlap and just keep moving forward.

Write your own memcpy() and memmove()?
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!