作者主页:paper jie的博客_CSDN博客
本文作者:大家好,我是paper jie,感谢你阅读本文,欢迎一建三连哦。
本文录入于《系统解析C语言》专栏,本专栏是针对于大学生,编程小白精心打造的。笔者用重金(时间和精力)打造,将算法基础知识一网打尽,希望可以帮到读者们哦。
其他专栏:《算法详解》《C语言》《C语言-语法篇》等
内容分享:本期将对c语言中的内存操作函数进行详细的讲解,各位看官姥爷快搬好小板凳坐好叭。
-------- 不要998,不要98,只要一键三连,三连买不了吃亏,买不了上当
目录
memcpy
memcpy的介绍和使用
memcpy的模拟实现
memmove
memmove的介绍和使用
memmove的模拟实现
memcmp
memcmp的介绍和使用
memset
memset的介绍和使用
void * memcpy ( void * destination, const void * source, size_t num )
Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.The underlying type of the objects pointed to by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.The function does not check for any terminating null character in source - it always copies exactly num bytes.To avoid overflows, the size of the arrays pointed to by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).
函数memcpy从source这个位置开始向后复制num个字节的数据到destination的内存位置
这个函数在遇到'\0'的时候并不会停止
如果source和destination有任何的重叠,复制的结果都是未定义的
解释:这里的函数参数之所以是void*,是为了适用于多种类型,可以接受多种类型的参数。且不能将一个数组的地址作为两个参数传给memcpy,不然复制的结果就是乱糟糟的,不可预测。
//memcpy的使用
#include
#include
int main()
{
int arr1[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
int arr2[] = { 1,1,1,1,1 };
memcpy(arr1, arr2, 20);
for (int i = 0; i < 8; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
//memcpy的模拟实现
#include
#include
#include
void* my_memcpy(void* dest, const void* source, size_t num)
{
void* ret = dest;
assert(dest && source);
while (num--)
{
*(char*)dest = *(char*)source;
dest = (char*)dest + 1;
source = (char*)source + 1;
}
return ret;
}
int main()
{
int arr1[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
int arr2[] = { 1,1,1,1,1 };
my_memcpy(arr1, arr2, 20);
for (int i = 0; i < 8; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
void * memmove ( void * destination, const void * source, size_t num )
Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.The function does not check for any terminating null character in source - it always copies exactly num bytes.To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.
memmove是memcpy的优化,和memcpy的区别就是memmove函数处理的原内存块和目标内存块可以重叠
如果源空间和目标空间出现重叠,就得用memmove函数处理
//memmove的使用
#include
#include
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
memmove(arr1, arr1 + 3, 20);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
//memmove的模拟实现
#include
#include
#include
void* my_memmove(void* dest, const void* source, size_t num)
{
void* ret = dest;
assert(dest && source);
while (num--)
{
if (dest < source) //前->后
{
*(char*)dest = *(char*)source;
dest = (char*)dest + 1;
source = (char*)source + 1;
}
else if (dest > source) //后->前
{
*((char*)dest + num) = *((char*)source + num);
}
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr1, arr1 + 3, 20);
for (int i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
int memcmp ( const void * ptr1, const void * ptr2, size_t num )
compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not.Notice that, unlike strcmp, the function does not stop comparing after finding a null character.
比较它们从prt1和prt2指针开始的num个字节,相同返回0,prt1
//memcmp的使用
#include
#include
int main()
{
char arr1[] = {1,2,3,4,5,6,7};
char arr2[] = {1,2,3,4,2};
int ret = memcmp(arr1, arr2, 20);
printf("%d\n", ret);
return 0;
}
void * memset ( void * ptr, int value, size_t num )
Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char)
从ptr指向的内存块开始向后num个字节,改为value的内容
//memset的使用
#include
#include
int main()
{
char arr[] = "hello world";
memset(arr, '0', 3);
printf("%s\n", arr);
return 0;
}