void* my_memcopy(void* dest, const void* src, size_t num)
{
if(dest == NULL || src == NULL)
{
return NULL;
}
char* ch_dest = (char*)dest;
char* ch_src = (char*)src;
for(int i = 0; i < num; i++)
{
ch_dest[i] = ch_src[i];
}
return dest;
}
void* my_memmove(void* dest, const void* src, size_t num)
{
if(dest == NULL || src == NULL)
{
return NULL;
}
char* ch_dest = (char*)dest;
const char* ch_src = (const char*)src;
if(ch_dest >= ch_src && ch_dest <= ch_src + num)
{
for(int i = num - 1; i >= 0; i--)
{
ch_dest[i] = ch_src[i];
}
}
else if(ch_dest <= ch_src && ch_dest + num >= ch_src)
{
for(int i = 0; i < num; i++)
{
ch_dest[i] = ch_src[i];
}
}
return dest;
}
合并成mycopy
void* mycopy(void* dest, const void* src, size_t num)
{
if(dest == NULL || src == NULL)
{
return NULL;
}
char* ch_dest = (char*)dest;
const char* ch_src = (const char*)src;
if(ch_dest >= ch_src && ch_dest <= ch_src + num)
{
for(int i = num - 1; i >= 0; i--)
{
ch_dest[i] = ch_src[i];
}
}
else
{
for(int i = 0; i < num; i++)
{
ch_dest[i] = ch_src[i];
}
}
return dest;
}