memmove function

Oct 23 2023 9:39 AM

I am trying to replicate the memmove function. I copied the following from online, and it's replicating the memmove function. But I am wondering, why we are checking source and destination memory addresses (if statement)? why that check is needed? is there any chance for overlap? If yes, how the destination and source string will overlap? memmove function has to copy the source string to the destination string in a non-destructive manner. Since both are stored in a different memory address how do both inrfere? where does the destruction happen? 

void    *ft_memmove(void *dst, const void *src, size_t len)

{

    char        *dst_aux;

    char        *src_aux;

    char        *dst_last;

    char        *src_last;

 

    if (!dst && !src)

        return (NULL);

    dst_aux = (char *)dst;

    src_aux = (char *)src;

    if (dst_aux < src_aux)

        while (len--)

            *dst_aux++ = *src_aux++;

    else

    {

        dst_last = dst_aux + (len - 1);

        src_last = src_aux + (len - 1);

        while (len--)

            *dst_last-- = *src_last--;

    }

    return (dst);

}


Answers (1)