前言:strcpy , strcmp 等的函数是针对字符串 来实现相应的功能的,如果想要对其他类型的数据进行相应功能的实现,是会受到限制,而memcpy,memmove,memcmp 是可以对内存块来实现对应功能。

注意:使用以下的函数时,都需要包含头文件 <string.h>
一 ,memcpy 使用和模拟实现
1.1 前言
以下代码可以使用strcpy 函数拷贝到arr2 中:
#include <string.h>
int main()
{
char arr1[] = "abcdef";
char arr2[20] = { 0 };
strcpy(arr2, arr1);
printf("%s\n", arr2);
return 0;
}
strcpy 函数只允许char 类型的字符串之间的拷贝, 如果有两个整型数组或者是其他类型的数据想相互之间进行拷贝 ,strcpy 是不能够实现的 ,而memcpy 却能够。
1.2 memcpy 的使用

memcpy
mem -- memory -- 记忆 -- 内存
memcpy --- 完成的是内存块的拷贝
- 函数memcpy 从source 的位置开始向后复制num 个字节的数据到destination指向的内存位置
- 这个函数在遇到 \0 的时候不会停下来
- 如果source 和destination 有任何重叠 , 复制的结果都是未定义的
#include <string.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[20] = { 0 };
memcpy(arr2, arr1, 5 * sizeof(int));
return 0;
}

1.3 memcpy 的模拟实现
#include <assert.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
//不知道是什么类型,就一个字节一个字节进行拷贝
while (num--)
{
//拿一个字节进行拷贝
*(char*)dest = *(char*)src;
//再进行下一个字节拷贝
//强制类型转化是临时的
dest = (char*)dest + 1; // void* = char* + 1
src = (char*)src + 1;
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4 };
int arr2[5] = { 0 };
my_memcpy(arr2, arr1, 2 * sizeof(int));
return 0;
}

1.4 重叠内存拷贝
#include <assert.h>
void* my_memcpy(void* dest, void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
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,8,9,10 };
// 1 2 1 2 3 4 5 8 9 10
my_memcpy(arr1+2, arr1, 5 * sizeof(int));
return 0;
}


这是因为在拷贝数据的时候,内存重叠了,如果在vs里使用memcpy 可以实现吗?
#include <string.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
// 1 2 1 2 3 4 5 8 9 10
memcpy(arr1+2, arr1, 5 * sizeof(int));
return 0;
}

memcpy 是不处理重叠情况的 , 但是vs 里可以实现,这是因为vs 这个编译器的功能强大, 但是并不代表其他编译器可以,为了代码的正确性,不要使用memcpy 来进行重叠内存的拷贝 , 可以使用memmove
二 ,memmove 使用和模拟实现
- 和memcpy 的差别就是memmove函数的处理的源内存块和目标内存块是可以重叠的
- 如果源内存块和目标空间出现重叠,就得使用memmove函数处理

2.1 memmove 的使用
#include <string.h>
int main()
{
int arr1[] = { 1, 2 , 3 , 4 ,5 , 6 , 7 , 8 , 9 , 10};
// 1 2 1 2 3 4 5 8 9 10
memmove(arr1+2, arr1, 5 * sizeof(int));
return 0;
}

2.2 memmove 的模拟实现
在使用memcpy 进行重叠内存的数据拷贝的时候,因为会存在数据覆盖,改变了某个内存空间的值,然后再传递的时候,传递不到原来的数据。因为内存重叠,原数据被覆盖,那么会想到,如果不让原数据被覆盖,即可实现memmove的效果。
拷贝一份原数据
改变传值顺序





#include <assert.h>
//实现内存块的拷贝,具体拷贝什么数据其实是未知的
void* my_memmove(void* dest,const void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
if (dest < src)
{
//前 -->后
while (num--)
//num-- 控制了循环次数
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else {
//后 --> 前
while (num--)
//num-- 使指针不断往前走
{
*((char*)dest + num) = *((char*)src + num);
//dest , src 虽然没变,但是num 一直在变小,指针一直往前走
}
}
return ret;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr+2 , arr, 5 * sizeof(int));
return 0;
}

三 ,memset 函数的使用
void * memset ( void * ptr, int value, size_t num );
memsrt -- memory set 内存设置
memset 是用来设置内存的,将内存中的值以字节为单位设置成想要的内容

例题1: 实现数组元素的全部置0
#include <string.h>
int main()
{
int arr[10] = { 0 };
for (int i = 0; i < 10; i++)
{
arr[i] = i + 1;
}
//实现全部元素置0
//法一:for 循环
/*for (int i = 0; i < 10; i++)
{
arr[i] = 0;
}*/
//法二:memset 置0
memset(arr, 0, 40);
return 0;
}
思考:能否实现数组全部元素的置1?

实现不了想要的结果,因为内存中的值以字节为单位设置想要的内容!

例题 2:
#include <string.h>
int main()
{
char arr[] = "hello world";
memset(arr,'x',5);
return 0;
}

四 ,memcmp 函数的使用

//memory cmpare 内存块的比较
#include <string.h>
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[] = { 1,2,3,3 };
int ret = memcmp(arr1, arr2, 13);
printf("%d\n", ret);
return 0;
}
如果比较的字节数过多,但是在前面的某个字节时已经比较出大小了,就会返回对应的结果,不会再继续的进行比较。