void * memset ( void * ptr, int value, size_t num );
memset(内存设置)
以字节为单位设置内存.
- ptr:起始地址
- value:设置的值
- num:多少字节
因为是以字节为单位,所以不能把整型数组改为1,但可以让数组初始化为0.
int main()
{
int arr[10] ={ 0 };
memset(arr, 1, 40);
return 0;
}
标准规定:memcpy实现不重叠内存的拷贝,重叠的内存拷贝由memmove实现
void * memcpy ( void * destination, const void * source, size_t num )
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t num)
{
void* ret = dest;
assert(dest && src);
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return ret;
}
void * memmove ( void * destination, const void * source, size_t num )
dest<src从前往后拷贝
dest>src从后往前拷贝
#include<assert.h>
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--)
{
*((char*)dest + num) = *((char*)src + num);
}
}
return ret;
}