void * memcpy ( void * destination, const void * source, size_t num );
memcpy拷贝整形数组
#include
#include
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
memcpy(arr2, arr1, 5 * sizeof(int));
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}
memcpy函数拷贝字符型数组
#include
#include
int main()
{
char arr1[] = "hello";
char arr2[10] = { 0 };
memcpy(arr2, arr1, 5);
int i = 0;
for (i = 0; i < 5; i++)
{
printf("%c ", arr2[i]);
}
return 0;
}
void* memcpy(void* dst, const void* src, size_t count)
{
void* ret = dst;
assert(dst);
assert(src);
/*
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return(ret);
}
如果source和destination有任何的重叠,复制的结果都是未定义的
#include
#include
void* my_memcpy(void* dst, const void* src, size_t count)
{
void* ret = dst;
assert(dst);
assert(src);
/*
* copy from lower addresses to higher addresses
*/
while (count--)
{
*(char*)dst = *(char*)src;
dst = (char*)dst + 1;
src = (char*)src + 1;
}
return(ret);
}
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
my_memcpy(arr1 + 2, arr1, 5 * sizeof(int));
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
void * memmove ( void * destination, const void * source, size_t num );
#include
#include
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 0 };
memmove(arr1 + 2, arr1, 5 * sizeof(int));
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
return 0;
}
运行结果如图:
memmove的模拟实现:
这里要分情况讨论,因为要把同一块空间前面的内容拷贝到后面,还是把后面的内容拷贝到前面是不同的情况,那应该怎么做呢?其实,很简单这里只需要记住要把前面的内容拷贝到后面就从后往前拷贝,即dest > src的时候,要把后面的内容拷贝到前面就从前往后拷贝,即dest < src的时候。
#include
#include
void* my_memmove(void* dest,const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
if (dest < src)
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
while (num--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
my_memmove(arr, arr + 2, 5 * sizeof(int));
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
}
void * memset ( void * ptr, int value, size_t num );
memset是⽤来设置内存的,将内存中的值以字节为单位设置成想要的内容。
#include
#include
int main()
{
char arr[20] = "hello world";
memset(arr, 'x', 5);
printf("%s\n", arr);
return 0;
}
#include
#include
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
memset(arr, 0, 10 * sizeof(int));
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
int memcmp ( const void * ptr1, const void * ptr2, size_t num );
#include
#include
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[] = { 1,2,3,4,8 };
int ret = memcmp(arr1, arr2, 4 * sizeof(int));
printf("%d\n", ret);
return 0;
}
#include
#include
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[] = { 1,2,3,4,8 };
int ret = memcmp(arr1, arr2, 17);
printf("%d\n", ret);
}
#include
#include
int main()
{
char buffer1[] = "DWgaOtP12df0";
char buffer2[] = "DWGAOTP12DF0";
int n;
n = memcmp(buffer1, buffer2, sizeof(buffer1));
if (n > 0)
printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
else if (n < 0)
printf("'%s' is less than '%s'.\n", buffer1, buffer2);
else
printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
return 0;
}