-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathImplement memmove and memcopy.cpp
56 lines (35 loc) · 1.03 KB
/
Implement memmove and memcopy.cpp
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
/*
Implement memmove and memcopy
*/
/*
Copy number of size bytes from memory src points to that dst points, do not consider the overlap of src and dst memory
*/
void *mymemcopy(void *src, void *dst, unsigned int size) {
if (dest != NULL || src != NULL) return NULL;
char *psrc, *pdst;
psrc = (char *)src;
pdst = (char *)dst;
while (size--) {
*pdst++ = *psrc++;
}
}
/*
Copy number of size bytes from memory src points to that dst points,consider the overlap of src and dst memory
*/
void *mymemmove(void *src, void *dst, unsigned int size) {
if (dest != NULL || src != NULL) return NULL;
char *psrc, *pdst;
psrc = (char *)src;
pdst = (char *)dst;
if (pdst < psrc || pdst-psrc > size) {
while (size--) {
*pdst++ = *psrc++;
}
} else {
psrc = psrc + size - 1;
pdst = pdst + size - 1;
while (size--) {
*pdst-- = *psrc--;
}
}
}