memcpy和memmove的区别与实现
来源:互联网 发布:unity3d学院 编辑:程序博客网 时间:2024/04/29 02:46
memcpy和memmove函数的实现
代码:
;***
;memcpy.asm - contains memcpy and memmove routines
;
; Copyright (c) 1986-1997, Microsoft Corporation. All right reserved.
;
;Purpose:
; memcpy() copies a source memory buffer to a destination buffer.
; Overlapping buffers are not treated specially, so propogation may occur.
; memmove() copies a source memory buffer to a destination buffer.
; Overlapping buffers are treated specially, to avoid propogation.
;
;*******************************************************************************
;***
;memcpy - Copy source buffer to destination buffer
;
;Purpose:
; memcpy() copies a source memory buffer to a destination memory buffer.
; This routine does NOT recognize overlapping buffers, and thus can lead
; to propogation.
; For cases where propogation must be avoided, memmove() must be used.
;
; Algorithm:
void* memcpy(void* dest, void* source, size_t count)
{
void* ret = dest;
//copy from lower address to higher address
while (count--)
*dest++ = *source;
return ret;
}
memmove
memmove - Copy source buffer to destination buffer
;
;Purpose:
; memmove() copies a source memory buffer to a destination memory buffer.
; This routine recognize overlapping buffers to avoid propogation.
; For cases where propogation is not a problem, memcpy() can be used.
;
; Algorithm:
void* memmove(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* mymemcpy( void* dest, const void* src, size_t count )
{
char* d = (char*)dest;
const char* s = (const char*)src;
// int n = (count + 7) / 8; // count > 0 assumed
int n = count >> 3;
switch( count & 7 )
{
do { *d++ = *s++;
case 7: *d++ = *s++;
case 6: *d++ = *s++;
case 5: *d++ = *s++;
case 4: *d++ = *s++;
case 3: *d++ = *s++;
case 2: *d++ = *s++;
case 1: *d++ = *s++;
case 0 } //while (--n > 0);
while (n-- > 0)
}
return dest;
}
- memcpy和memmove的区别与实现
- memcpy和memmove的区别与实现
- memcpy和memmove的区别与实现
- sizeof()与strlen()的区别 memcpy memmove区别和实现
- memcpy与memmove区别和实现
- memcpy函数和memmove函数的模拟实现与区别
- memcpy和memmove的区别与联系
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- memcpy memmove区别和实现
- 你不是个好男人
- 类的构造函数、析构函数与赋值函数
- 杂记
- 散列 (Hashing)
- 累了,就进来看笑话
- memcpy和memmove的区别与实现
- 如何对系统中的某个进程进行监控
- [大话IT]IT人士之成功之6大步骤(转载)
- wincvs与cvsnt
- Comet (Server Push)
- 类的大小
- vc内嵌汇编传参的问题
- 影响中国年度十大猛人
- 主函数main中变量(int argc,char *argv[])的含义