bcopy correctly handles overlapping fields, while the behavior of memcpy is undefined if the source and destination overlap. The ANSI C memmove function must be used when the fields overlap.
在书里看到这两句话的时候挺困惑的,平时用这几个函数也没有留意这个问题,于是写了些代码验证了下这个问题。
#include <stdio.h>
#include <string.h>
void main()
{
int test[10];
int* source = test;
int* destination = &test[2];
int i = 0;
for (i = 0; i < 10; ++i)
{
test[i] = i;
}
memcpy(destination, source, sizeof(int) * 5); // 0 1 0 1 2 3 4 7 8 9
memcpy(source, destination, sizeof(int) * 5); // 2 3 4 5 6 5 6 7 8 9
bcopy(source, destination, sizeof(int) * 5); // 0 1 0 1 2 3 4 7 8 9
bcopy(destination, source, sizeof(int) * 5); // 2 3 4 5 6 5 6 7 8 9
memmove(destination, source, sizeof(int) * 5); // 0 1 0 1 2 3 4 7 8 9
memm