Info
The function copies a block of memory from one location to another, ensuring safe handling of overlapping source and destination areas by using a temporary buffer. It guarantees the correct order of copying, unlike
memcpy
, which may corrupt data if the regions overlap.
Walkthrough
Moving memory from point A to point B is pretty straightforward if you’ve reached this exercise. We cast void
parameters to unsigned char
and move char
by char
until (n--)
. However, the key focus of this exercise lies in safely handling overlapping source and destination areas, which we’re going to cover here.
Attention
You can still close this tab. Consider yourself warned xx
void *ft_memmove(void *dest, const void *src, size_t n)
{
unsigned char *d;
const unsigned char *s = src;
d = dest;
if (d == s)
{
return (dest);
}
if (d < s)
{
while (n--)
{
*d++ = *s++;
}
}
else
{
d += n;
s += n;
while (n--)
{
*(--d) = *(--s);
}
}
return (dest);
}
Explanation
Why would we copy backward if destination is after source? Let’s consider the following scenario:
So,
- If
dst > src
, copy backward to avoid overwritingsrc
- If
dst < src
, copy forward as no overlap risk exists