内存相关函数

  • memcpy

void *memcpy( void *dest, const void *src, size_t count );

Remarks

The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions.//注意内存重叠--标准未定义(无意义)

void*  mymemcpy(void* dest, void* src, int count)//不考虑内存重叠
{
	void* ret = dest;
	while (count!=0)
	{
		*(char*)dest= *(char*)src;
		dest = (char *)dest + 1;
		src = (char *)src + 1;
		count--;
	}
	return ret;
}
  • memmove
  • 他们的作用是一样的,唯一的区别是,当内存发生局部重叠的时候,memmove保证拷贝的结果是正确的,memcpy不保证拷贝的结果的正确。

void *memmove( void *dest, const void *src, size_t count );//可解决内存重叠问题

void * mymemmove(void *dest, const void *src, int count)
{
	void* ret = dest;
	if (src > dest)
	{
		while (count != 0)
		{
			*(char*)dest = *(char*)src;
			dest = (char *)dest + 1;
			src = (char *)src + 1;
			count--;
		}
	}
	else
	{
		count--;
		while (count >= 0)
		{
			*((char*)dest + count) = *((char*)src + count);
			count--;
		}
	}
	return ret;
}

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(C语言日常总结,c语言)