memcpy
这个函数在遇到’\0’的时候并不会停下来
#include<stdio.h>
//函数拷贝结束后,返回目标空间的起始地址
void* my_memcpy(void* dest, const void* src, size_t num)
{void* ret = dest;assert(src && dest);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 };//01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00 06 00 00 00int arr2[20] = { 0 };my_memcpy(arr2, arr1, 21);int i = 0;for (i = 0; i < 20; i++){printf("%d ", arr2[i]);}return 0;
}
memmove
与memcpy的区别在于memmove函数处理的源内存块和目标内存块是可以重叠的
void* my_memmove(void* dest, const void* src, size_t num)
{void* ret = dest;assert(dest && src);if (dest < src){//前->后while (num--){*(char*)dest = *(char*)src;dest = (char*)dest + 1;src = (char*)src + 1;}}else{//后->前while (num--)//20{*((char*)dest + num) = *((char*)src + num);}}return ret;
}
memset
以字节为单位设置
#include<stdio.h>
int main()
{char arr[] = "hello bit";memset(arr+1,'x',4);//以字节为单位设置的printf("%s\n", arr);return 0;
}//int main()
//{
// int arr[10] = { 0 };
// memset(arr, 1, 40);
//
// return 0;
//}