目录
文章目录
内存函数针对的数据类型不确定可能是整型数据,字符数据,结构体数据......
memcpy的使用和模拟实现
memcpy的使用
memcpy打印字符数据
memcpy打印整型数据
memcpy的模拟实现
模拟实现的memcpy打印重叠情境
memmove的使用和模拟实现
memmove的使用
memove的模拟实现
memset函数的使用和模拟实现
memset的使用
memset的模拟实现
memcmp的使用和模拟实现
memcmp的使用
memcmp的模拟实现
我们先看一下菜鸟教程中对memcpy的解释
void *memcpy(void *str1, const void *str2, size_t n)
#include
#include
int main()
{
char arr[100] = { 0 };
char arr2[50] = "woshi shuaige";
memcpy(arr, arr2, 7);
int i = 0;
for (i = 0; i < 7; i++)
{
printf("%c", arr[i]);
}
return 0;
}
#include
#include
int main()
{
int arr[100] = { 0 };
int arr2[50] = {1,2,3,4,5,6,7,8,9,10};
memcpy(arr, arr2, 20);
int i = 0;
for (i = 0; i < 5; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
memcpy最好不要处理重叠的内存,对于重叠的内存,我们交给memmove来处理
#include
#include
void* mymemcpy(void* str1, const void* str2, size_t n)
{
void* ret = str1;
assert(str1, str2);
while (n--)
{
*(char*)str1 = *(char*)str2;
str1 = (char*)str1 + 1;
str2 = (char*)str2 + 1;
}
return ret;
}
int main()
{
char arr[100] = { 0 };
char arr2[50] = "woshi shuaige";
mymemcpy(arr, arr2, 7);
int i = 0;
for (i = 0; i < 7; i++)
{
printf("%c", arr[i]);
}
return 0;
}
void* mymemcpy(void* str1, const void* str2, size_t n)
{
void* ret = str1;
assert(str1, str2);
while (n--)
{
*(char*)str1 = *(char*)str2;
str1 = (char*)str1 + 1;
str2 = (char*)str2 + 1;
}
return ret;
}
int main()
{
int arr[50] = {1,2,3,4,5,6,7,8,9,10};
mymemcpy(arr+2, arr, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
void * memmove ( void * destination, const void * source, size_t num );
#include
#include
int main()
{
int arr[50] = {1,2,3,4,5,6,7,8,9,10};
memmove(arr+3, arr, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
#include
#include
void* mymemmove(void* destination, const void* source, size_t num)
{
void* ret = destination;
assert(destination && source);
if (destination < source)
{
while (num--)
{
*(char*)destination = *(char*)source;
destination = (char*)destination + 1;
source = (char*)source + 1;
}
}
else
{
while (num--)
{
*((char*)destination + num) = *((char*)source + num);
}
}
return ret;
}
int main()
{
int arr[50] = { 1,2,3,4,5,6,7,8,9,10 };
mymemmove(arr+2, arr, 20);
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
void * memset ( void * ptr, int value, size_t num );
#include
#include
int main()
{
char arr[50] = "woshi shuaige";
memset(arr, 'x', 6);
printf("%s", arr);
return 0;
}
注意memset在设置的时候,是以字节为单位来设置的。
#include
void* mymemset(void* ptr, int value, size_t num)
{
void* ret = ptr;
int i = 0;
for (i = 0; i < num; i++)
{
*((char*)ptr + i) = 'x';
}
return ret;
}
int main()
{
char arr[50] = "woshi shuaige";
mymemset(arr, 'x', 5);
printf("%s", arr);
return 0;
}
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include
#include
int main()
{
char arr[100] = "woshi shuaige";
char arr1[50] = "woshi shuaibi";
int ret=memcmp(arr, arr1, 12);
printf("%d\n", ret);
return 0;
}
#include
int mymemcmp(const void* ptr1, const void* ptr2, size_t num)
{
while (num--)
{
if (*(char*)ptr1 > *(char*)ptr2)
{
return 1;
}
else if (*(char*)ptr1 < *(char*)ptr2)
{
return -1;
}
else
{
ptr1 = (char*)ptr1 + 1;
ptr2 = (char*)ptr2 + 1;
}
}
return 0;
}
int main()
{
char arr[100] = "woshi shuaige";
char arr1[50] = "woshi shuaibi";
int ret = mymemcmp(arr, arr1, 12);
printf("%d\n", ret);
return 0;
}