Find the answer to your Linux question:
Results 1 to 5 of 5
Hi , I gone through man page of memcpy and memmove , from there i got one difference as :- memcpy :- The memory areas should not overlap. Use mem‐ ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2007
    Location
    Bangalore >> India
    Posts
    28

    Exclamation What is the difference between memcpy and memmove ?


    Hi ,

    I gone through man page of memcpy and memmove , from there i got one difference as :-

    memcpy :- The memory areas should not overlap.
    Use mem‐ move(3) if the memory areas do overlap.

    memmove:-The memory areas may overlap

    But, it's not happening in practical , for this , please have a look the given example ,



    Waiting solution with good examples.

    Thanks,
    -Amaresh
    Attached Files Attached Files

  2. #2
    Just Joined!
    Join Date
    Dec 2007
    Location
    Bangalore >> India
    Posts
    28
    Code is :--
    amaresh@eOdissa:~/Documents/Test$ echo ;echo ;echo "----------------memcpy.c------------------";cat memcpy.c ; ./memcpy ;echo "-------------memmove.c------------------";cat memmove.c ;echo "Result:-"; ./memmove


    ----------------memcpy.c------------------
    #include<stdio.h>
    #include<string.h>
    int
    main()
    {
    char str1[]="123456789";
    char str2[]="abcdefghi";
    memcpy(str1+1,str2+1,4);
    printf("memcpy= %s\n",str1);
    return 0;
    }
    memcpy= 1bcde6789
    -------------memmove.c------------------
    #include<stdio.h>
    #include<string.h>
    int
    main()
    {
    char str1[]="123456789";
    char str2[]="abcdefghi";
    memcpy(str1+1,str2+1,4);
    printf("Result String: = %s\n",str1);
    return 0;
    }
    Result:-
    Result String: = 1bcde6789

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,530
    The variables str1 and str2 are not overlapping memory segments. Hence, memcpy and memmove appear to work the same. If you were to do this:

    memmove(str1+1, str1+0, 4)
    vs.
    memcpy(str1+1, str1+0, 4)

    Then you would have a problem with memcpy since it would be writing over itself.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  4. #4
    Just Joined!
    Join Date
    Dec 2007
    Location
    Bangalore >> India
    Posts
    28

    Red face

    See the bold letters.. which gives same answer...

    Quote Originally Posted by amaresh_83 View Post
    Code is :--
    amaresh@eOdissa:~/Documents/Test$ echo ;echo ;echo "----------------memcpy.c------------------";cat memcpy.c ; ./memcpy ;echo "-------------memmove.c------------------";cat memmove.c ;echo "Result:-"; ./memmove


    ----------------memcpy.c------------------
    #include<stdio.h>
    #include<string.h>
    int
    main()
    {
    char str1[]="123456789";
    char str2[]="abcdefghi";
    memcpy(str1+1,str2+1,4);
    printf("memcpy= %s\n",str1);
    return 0;
    }
    memcpy= 1bcde6789


    -------------memmove.c------------------
    #include<stdio.h>
    #include<string.h>
    int
    main()
    {
    char str1[]="123456789";
    char str2[]="abcdefghi";
    memcpy(str1+1,str2+1,4);
    printf("Result String: = %s\n",str1);
    return 0;
    }
    Result:-
    Result String: = 1bcde6789
    Quote Originally Posted by Rubberman View Post
    The variables str1 and str2 are not overlapping memory segments. Hence, memcpy and memmove appear to work the same. If you were to do this:

    memmove(str1+1, str1+0, 4)
    vs.
    memcpy(str1+1, str1+0, 4)

    Then you would have a problem with memcpy since it would be writing over itself.

  5. #5
    Linux Engineer GNU-Fan's Avatar
    Join Date
    Mar 2008
    Posts
    935
    Quote Originally Posted by amaresh_83 View Post
    See the bold letters.. which gives same answer...
    This isn't what Rubberman was talking about. Overlapping means you copy characters from a place that gets overwritten in the progress of copying.
    Like you have "1234" and want to copy the "34" part to the position of "23".
    In that case the position of "3" is overlapping.

    You used memcpy in both case, by the way.
    Debian GNU/Linux -- You know you want it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •