-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_memmove.c
58 lines (49 loc) · 2.22 KB
/
ft_memmove.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amait-ou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/29 18:10:12 by amait-ou #+# #+# */
/* Updated: 2023/01/24 00:07:43 by amait-ou ### ########.fr */
/* */
/* ************************************************************************** */
/*
we have created "ft_memcpy" but that last doesn't care at all about the
overlap as it returns undefined, this my "ft_memmove" has been created to
handle this problem.
in order to copy from a source into a destination there is only three cases
case 1:
when the "src" and "dst" point at the same block of memory, in this
case there is no need to perform a copying operation since they are
the same.
case 2:
when the "src" is greater than the "dst", in this case you might
want to use memcpy as an option os just loop over the src and copy
n elements starting from the beginning.
case 3:
when the "dst" is greater than "src", here is why "ft_memmove"
was made to handle what called "Memory Overlap" where we lost
the data by overwriting it. memmove use a temporary memory allocation
to store tha data to be copied from "src "to "dst", and since
the malloc is forbidden in this part, there is another way to copy
from "src" to "dst" without overlaping by using the copying from len
to 0 (reversely) and this method is powerful to avoid "Memory Overlaping"
*/
#include "./superlib.h"
void *ft_memmove(void *dst, const void *src, size_t len)
{
char *d;
char *s;
d = (char *)dst;
s = (char *)src;
if (d > s)
{
while (len--)
*(d + len) = *(s + len);
}
else
ft_memcpy(d, s, len);
return ((void *)d);
}