目录
内存函数针对的数据类型不确定可能是整型数据,字符数据,结构体数据......
内存函数针对的数据类型不确定可能是整型数据,字符数据,结构体数据......
memcpy的使用和模拟实现
我们先看一下菜鸟教程中对memcpy的解释
void *memcpy(void *str1, const void *str2, size_t n)
memcpy的使用
memcpy打印字符数据
#include <stdio.h>
#include <string.h>
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;
}
memcpy打印整型数据
#include <stdio.h>
#include <string.h>
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来处理
memcpy的模拟实现
#include <stdio.h>
#include <assert.h>
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;
}
模拟实现的memcpy打印重叠情境
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;
}
memmove的使用和模拟实现
void * memmove ( void * destination, const void * source, size_t num );
memmove的使用
#include <stdio.h>
#include <string.h>
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;
}
memove的模拟实现
#include <stdio.h>
#include <assert.h>
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;
}
memset函数的使用和模拟实现
void * memset ( void * ptr, int value, size_t num );
memset的使用
#include <stdio.h>
#include <string.h>
int main()
{
char arr[50] = "woshi shuaige";
memset(arr, 'x', 6);
printf("%s", arr);
return 0;
}
注意memset在设置的时候,是以字节为单位来设置的。
memset的模拟实现
#include <stdio.h>
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;
}
memcmp的使用和模拟实现
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
memcmp的使用
#include <stdio.h>
#include <string.h>
int main()
{
char arr[100] = "woshi shuaige";
char arr1[50] = "woshi shuaibi";
int ret=memcmp(arr, arr1, 12);
printf("%d\n", ret);
return 0;
}
memcmp的模拟实现
#include <stdio.h>
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;
}