What does memmove actually do?

This is a discussion on What does memmove actually do? within the C Programming forums, part of the General Programming Boards category; When I research memmove online I get tons of websites telling me the difference between memmove and memcopy. I know ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Location
    CA
    Posts
    19

    Exclamation What does memmove actually do?

    When I research memmove online I get tons of websites telling me the difference between memmove and memcopy. I know that memmove does the checks for overlapping memory that memcpy doesn't. I also have read how memmove will copy in the direction that does not overlap (when deciding whether to copy bytes right to left or left to right). But what about in the case where you are copying memory that is overlapping on both sides? For example, I'm trying to implement the vector and write the code for shifting down elements. When an element in the middle of the vector shifts down, it is surrounded by used-up memory on both sides. Does memmove just shift that entire chunk of memory after the place I'm inserting down? I'm getting some errors with the indices...

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So, you have like 5 elements in your vector. You want to shift down. You would start from the beginning moving the first element. Then the 2nd etc etc. You would do the moving with memmove().
    Now, I don't know exactly what you mean with shift down (like what will happen to the 1st element and the last?) but memove() seems to be ok.
    It probably WON'T move the entire block, meaning the entire remaining vector. It will just copy the element where you specify. If there is used-up memory, that memory will be overwritten.
    Like you have a vector of char.
    'a' 'b' 'c' 'd'
    You move shift down. The first elements lets say gets lost and the last becomes '0'. You will have:
    memmove(1,2) --> so 'b' 'b' 'c' 'd'
    memmove(2,3) --> so 'b' 'c' 'c' 'd'
    memmove(3,4) --> so 'b' 'c' 'd' 'd'
    4 = '0' --> so 'b' 'c' 'd' '0'
    That is what I understand with memmove() you will use sizeof(type_of_element) for the shifting offset. Here sizeof(char).
    memcpy() won't work because it overlaps some memory used (I don't know what will happen)

    A final note. I haven't ever used them, so I am just guessing here. Someone more experience should confirm (or slap me)

    EDIT: Let me answer though directly the question.
    Lets say you have:
    Code:
    char* str = "dude";
    memmove(str+1, str,3);
    printf("%s",str);
    you will probably have "ddud". What happens is that ['d']['u'['d'] is copied to a temporary buffer. Then that is copied where specified.
    Now if you used memcpy() instead of memmove() then NO temporary buffer is used. So this might happend:
    1) ['d']['u']['d']['e'] original
    2) ['d']['d']['d']['e'] after first copy (copies 1st element to the 2nd element)
    3) ['d']['d']['d']['e'] after second copy (copies 2nd element to the 3rd element)
    4) ['d']['d']['d']['d'] after third copy (copies 3nd element to the 4rd element)

    So you will have "dddd". Why? Because you write/read in the same place, so the reading data get altered by the writing data.
    Last edited by C_ntua; 10-17-2008 at 06:31 AM.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    CA
    Posts
    19
    great, thanks so much!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Quote Originally Posted by C_ntua View Post
    Code:
    char* str = "dude";
    memmove(str+1, str,3);
    printf("%s",str);
    Tsk, tsk. You should know better.
    That will (most likely) segfault. Otherwise, undefined behavior.
    Last edited by Elysia; 10-17-2008 at 12:13 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Elysia View Post
    Tsk, tsk. You should know better.
    That will segfault.
    That MIGHT segfault.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    OK then, I changed it. All better?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    1,691
    EDIT:
    But that was just an example. No one seriously writes to a pre-defined string.
    Last edited by Yarin; 10-17-2008 at 12:23 PM.
    A class that doesn't overload all operators just isn't finished yet. -- SmugCeePlusPlusWeenie
    A year spent in artificial intelligence is enough to make one believe in God. -- Alan J. Perlis

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,033
    Yeah, but as you know, this sort of code is... bad practice, and it most likely won't even work when compiled. And the post stated no such information that it was bad practice or didn't work...
    And a lot of people write code that stores string literals in a char* pointer, so it's worth mentioning.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by Elysia View Post
    Yeah, but as you know, this sort of code is... bad practice, and it most likely won't even work when compiled. And the post stated no such information that it was bad practice or didn't work...
    And a lot of people write code that stores string literals in a char* pointer, so it's worth mentioning.
    Actually, it is most likely going to "work" just fine. Which is even worse.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Then you get yourself a fancy memset().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memcpy Vs memmove
    By onebrother in forum C Programming
    Replies: 10
    Last Post: 08-03-2007, 05:41 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Removing single char from string
    By cboard_member in forum C Programming
    Replies: 17
    Last Post: 09-28-2005, 04:17 AM
  4. memmove problem
    By ChadJohnson in forum C++ Programming
    Replies: 4
    Last Post: 03-12-2005, 06:12 PM
  5. Realloc inappropriate for aligned blocks - Alternatives?
    By zeckensack in forum C Programming
    Replies: 2
    Last Post: 03-20-2002, 02:10 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21