Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why memmove() function works this way?

Tags:

c

memmove

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.

like image 272
Yatendra Rathore Avatar asked Jul 26 '17 14:07

Yatendra Rathore


People also ask

What does Memmove function do?

In the C Programming Language, the memmove function copies n characters from the object pointed to by s2 into the object pointed to by s1. It returns a pointer to the destination. The memmove function will work if the objects overlap.

How is Memmove implemented?

The memmove function copies n characters from the source to the destination object. In memmove before copying the characters from source to destination object first copied the n character from source to the temporary array, after that copy the n character from the temporary array to the destination object.

Why is Memmove faster than memcpy?

It copies 100 mb with memcpy, and then moves about 100 mb with memmove; source and destination are overlapping. Various "distances" for source and destination are tried. Each test is run 10 times, the average time is printed. Memmove is implemented as a SSE optimized assembler code, copying from back to front.

How memcpy function works?

memcpy() function in C/C++ The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string. h” header file in C language.


1 Answers

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.

like image 146
Sergey Kalinichenko Avatar answered Nov 07 '22 03:11

Sergey Kalinichenko