#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//memcpy内存拷贝函数
//void* memcpy(void* destination, const void* source, size_t num)
//memcpy负责拷贝两块独立空间中的数据
int main()
{
int arr1[] = { 1,2,3,4,5,6,7 };
int arr2[10] = { 0 };
memcpy(arr2, arr1, 28);
float arr3[5] = { 1.0, 2.5, 3.0, 5.0, 6.0 };
float arr4[10] = { 0.0 };
memcpy(arr4, arr3, 20);
return 0;
}
//memcpy的模拟实现
#include <assert.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;//一个字节一个字节拷贝
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7 };
int arr2[10] = { 0 };
my_memcpy(arr2, arr1, 28);
return 0;
}
//如果
/*
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
//将1,2,3,4,5的数据拷贝到3,4,5,6,7上
// 变成 1,2,1,2,3,4,5,8,9,10
my_memcpy(arr1+2, arr1, 20);
*/
//重叠内存的拷贝,是怎么实现的呢?
//这时候要用到memmove