原版:

void *memcpy( void *dest, const void *src, size_t count )
{
	ASSERT((dest != NULL)&&(src != NULL));
	char *temp_dest = (char *)dest;
	char *temp_src = (char *)src;
	while(count--)	// 不对是否存在重叠区域进行判断
	{
		*temp_dest++ = *temp_src++;
	}
	return dest;
}


改进一:

void* memcpy(void* dest, void* source, size_t count)

   {

       void* ret = dest;


      if (dest <= source || dest >= (source + count))

       {

          //Non-Overlapping Buffers
         //copy from lower addresses to higher addresses

         while (count --)

               *dest++ = *source++;

     }

     else

     {

        //Overlapping Buffers
       //copy from higher addresses to lower addresses


       dest += count - 1;

       source += count - 1;

       while (count--)

                *dest-- = *source--;l

     }

      return ret;

   }


改进二:

void memcpy(void* dest, const void* src, size_t n)

{

    assert((((uintptr_t) dest | (uintptr_t) src | n) & 0x03) == 0);

 

    uint32_t* d = (uint32_t*) dest;

    const uint32_t* s = (uint32_t*) src;

 

    n /= sizeof(uint32_t);

 

   if (d < s) {

        /* copy forward */

        while (n--) {

            *d++ = *s++;

        }

    } else {

        /* copy backward */

        d += n;

        s += n;

        while (n--) {

            *--d = *--s;

        }

    }

}