6

I was going through various questions related to memmove() vs memcpy() on Stack Overflow.

There was this post that implied that if the source address (data to be copied from) is greater than destination address (data to be copied to), it will perform forward copying otherwise it will perform backward copying.

Why it is that way? I tried forward copying for both cases and that worked perfectly fine without any error.

Can you please elaborate? I'm not getting the reason.

EDIT: here's what i meant to say:

#include <memory.h> 
#include <string.h>
#include <stdio.h>

void *memmoveCustom(void *dest, const void *src, size_t n)
{
    unsigned char *pd = (unsigned char *)dest;
    const unsigned char *ps = (unsigned char *)src;
    if ( ps < pd )
        for (pd += n, ps += n; n--;)
            *--pd = *--ps;
    else
        while(n--)
            *pd++ = *ps++;
    return dest;
}

int main( void )
{
    printf( "The string: %s\n", str1 );

    memmoveCustom( str1 + 1, str1, 9 );
    printf( "Implemented memmove output: %s\n", str1 );

    getchar();
}

Above program is an example code that is implementing a custom memmove() function. Inside the function, you can see when the source address was greater than destination address, it performed forward copying otherwise it added n-location to the existing addresses and performed the copying from backward. That's it.

Yatendra Rathore
  • 333
  • 2
  • 13
  • 2
    Please link to the post that you're referring to. – Blorgbeard Jul 26 '17 at 14:06
  • 2
    Did the buffers you tested with overlap? Because that's the main purpose of memmove. – DeiDei Jul 26 '17 at 14:07
  • 1
    `it will perform forward copying otherwise it will perform backward copying`. What does this even mean? That it will copy from address 0x000 to 0xFFF sometimes but othertimes will copy from 0xFFF to 0x000? Why would it need to do a reverse copy? – user9993 Jul 26 '17 at 14:08
  • 1
    "I tried forward copying for both cases and that worked perfectly fine without any error." --> show that code. Perhaps what you conclude as "perfectly fine" is not so. – chux - Reinstate Monica Jul 26 '17 at 14:14
  • "There was this post …" - which post? The standard does not make any requirement **how** the library functions have to accomplish their task. (and it typically is not that simple as you wrote). And what is your problem? It seems there is none, so what is unclear about the documentation? – too honest for this site Jul 26 '17 at 14:44
  • https://stackoverflow.com/a/22746165/8297714 for the above source code and https://stackoverflow.com/a/27774099/8297714 for the comment that signified it.(check the third comment). – Yatendra Rathore Jul 26 '17 at 16:01
  • BTW: `if ( ps < pd )` is UB, yet likely does what you want. – chux - Reinstate Monica Jul 26 '17 at 18:29

1 Answers1

15

if the source address(data to be copied from) is greater than destination address(data to be copied to), it will perform forward copying otherwise it will perform backward copying.

This is important only when from and to ranges overlap. For non-overlapping ranges the direction of copying does not matter.

It matters for overlapping ranges because if you start copying from the beginning, you will destroy the source data before you get to copying it.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Hmmm, I get the feeling that backwards copying would be very cache unfriendly. Am I right? – klutt Jul 26 '17 at 22:11