注:1、本文中所提到的4个内存操作函数的头文件均为<string.h>
2、本文中所用例子均为本人在VS2022 Debug X86的环境下测试所得
一、memcpy函数
1、函数中最前面的void*代表着该函数的返回类型可以为一个任意类型的指针。
2、该函数的作用就是从source的内存位置开始向后复制num个字节的数据到destination的内存位置。
3、该函数在复制的过程中即使遇到'\0'也不会停下,而是会继续复制,直到num个字节复制完。
注意:
(1)destination的空间要足够大 或者说 所要复制内容的大小不能超过destination的大小,否则程序会崩溃。
ex:

(2)如果所设置复制的字节数(num)超过source本身的大小,则后面会复制随机值,直到num个字节。
ex:

4、如果source和destination有任何的重叠,复制的结果都是未定义的
注:其实,C语言标准只是规定memcpy来实现不重叠的内存拷贝,但实际上发现在VS2022这个环境中的memcpy即使面对内存重叠的拷贝,它一样能够拷贝好,即VS2022中的memcpy能够超额完成任务。
ex:

memcpy函数模拟实现
#include <stdio.h>
#include <assert.h>
void *my_memcpy(void *destination, const void *source, size_t num)
{
assert(destination && source);
void *ret = destination;
while (num--)
{
// 细节问题:source 应该强转成 (const char*),而不是 (char*)。(虽然此处是读取操作,但破坏了类型系统的 const 安全性)
// *(char *)destination++ = *(const char *)source++; // 注:理论上这样写是有错的,因为 ++ 的优先级是高于 ()强制类型转化 和 * 的,所以 ++ 操作是最先进行的,但是 void* 类型的指针是不能进行 ++ 操作的。
// *(((char *)destination)++) = *(((const char *)source)++); // 注:这样写是有问题的,(char*)dest 和 (char*)src 是类型转换后的临时右值(rvalue),无法直接进行 ++ 操作(自增运算符要求操作数是左值),编译就会报错。但不知道为什么VS2022上这句代码是可以正常运行的(本人猜测可能是编译器优化的原因)。
// 下面的这种写法在两个编译器上都能正常跑
*((char *)destination) = *((const char *)source);
destination = (char *)destination + 1;
source = (const char *)source + 1;
}
return ret;
}
int main()
{
int arr1[10] = {0};
int arr2[] = {1, 2, 3, 4, 5};
my_memcpy(arr1, arr2, 20);
for (int i = 0; i < 10; ++i)
{
printf("%d ", arr1[i]);
}
printf("\n");
return 0;
}
二、memmove函数

1、函数中最前面的void*代表着该函数的返回类型可以为一个任意类型的指针。
2、该函数的功能和使用时的注意事项与memcpy类似,这里就不多重复了,唯一的差别就是memmove函数处理的源内存块和目标存块是可以重叠的,即如果源空间和目标空间出现重叠,就使用memmove函数处理。
注:memmove函数通俗一点讲它就是memcpy函数的升级版,源空间与目标空间无论重不重叠都可以使用该函数进行内存中数据的拷贝。
memmove函数模拟实现
// 初学者建议看这一份my_memmove
void *my_memmove(void *destination, const void *source, size_t num)
{
assert(destination && source);
void *ret = destination;
if ((char *)destination > (char *)source) // 那就从后往前进行拷贝
{
// // 先找到尾部
// destination = (char*)destination + num - 1; // 注意要减1,比如你就只要拷贝一个字节的时候,这里应该要是加0
// source = (char*)source + num - 1;
// while (num--) // 开始拷贝
// {
// *((char*)destination) = *((char*)source);
// destination = (char*)destination - 1;
// source = (char*)source - 1;
// }
// 最简洁优雅的写法
while (num--)
*((char *)destination + num) = *((char *)source + num);
}
else if ((char *)destination < (char *)source) // 那就从前往后拷贝
{
while (num--)
{
*((char *)destination) = *((char *)source);
destination = (char *)destination + 1;
source = (char *)source + 1;
}
}
return ret;
}
// 简洁优雅的写法
void *my_memmove(void *destination, const void *source, size_t num)
{
assert(destination && source);
char *dest = (char *)destination;
const char *src = (const char *)source;
if (dest < src)
while (num--)
*dest++ = *src++;
else
while (num--)
*(dest + num) = *(src + num);
return destination;
}
int main()
{
char arr[10] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', '\0'};
my_memmove(arr + 1, arr, 1);
for (int i = 0; i < 10; ++i)
{
printf("%c ", arr[i]);
}
printf("\n");
return 0;
}
三、memset函数

1、函数中最前面的void*代表着该函数的返回类型可以为一个任意类型的指针。
2、以字节为单位进行内存的设置的。
3、该函数的作用是设置内存的,将从ptr开始往后的num个字节的数据设置成value。
ex:

注意:
设置的时候要注意,设置的字节数不能超出ptr所指的范围了,否则程序会崩溃。
ex:

memset函数模拟实现
#include<stdio.h>
#include<assert.h>
void* my_memset(void* ptr, int value, size_t num)
{
assert(ptr);
void* ret = ptr;
while (num--)
{
*(char*)ptr = (char)value;
ptr = (char*)ptr + 1;
}
return ret;
}
int main()
{
char arr[] = "hello world";
my_memset(arr + 6, 'x', 3);
printf("%s\n", arr);
return 0;
}
四、memcmp函数

1、该函数的功能为比较ptr1和ptr2指针的前num个字节的内容。
2、比较规则:

如果ptr1<ptr2,则返回<0的数字。
如果ptr1==ptr2,则返回数字0。
如果ptr1>ptr2,则返回>0的数字。
注意:是以字节为单位来比较的。
ex:

通过调试打开内存窗口观察可以发现(一行代表4个字节,00两个数字为一个字节,因为这是以十六进制的形式表示的且VS2022数据采用的是小端存储模式)


arr1和arr2的前13个字节的内容是完全一样的,所以打印的结果为0。
memcmp函数模拟实现
#include<stdio.h>
#include<assert.h>
int my_memcmp(const void* ptr1, const void* ptr2, size_t num)
{
assert(ptr1 && ptr2);
while (num--)
{
if (*(char*)ptr1 != *(char*)ptr2)
{
return *(char*)ptr1 - *(char*)ptr2;
}
ptr1 = (char*)ptr1 + 1;
ptr2 = (char*)ptr2 + 1;
}
return 0;
}
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[] = { 1,2,3,0x11223304 };
printf("%d\n", my_memcmp(arr1, arr2, 13));
int arr3[] = { 1,2,3,5 };
printf("%d\n", my_memcmp(arr2, arr3, 13));
int arr4[] = { 1,2,3,3 };
printf("%d\n", my_memcmp(arr2, arr4, 13));
return 0;
}
文章到这就结束了,如果本文中有什么不足之处,希望您能在留言区留言指点,有什么问题也可以在留言区留言,谢谢。
2746




被折叠的 条评论
为什么被折叠?



