全部博文(160)
分类: C/C++
2014-11-04 23:41:19
//
// main.cpp
// AUTO_PRO
//
// Created by yanzhengqing on 12-12-11.
// Copyright (c) 2012年 yanzhengqing. All rights reserved.
//
#include
#include
using namespace std;
/***
*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.
*
*Entry:
* void *dst = pointer to destination buffer
* const void *src = pointer to source buffer
* size_t count = number of bytes to copy
*
*Exit:
* Returns a pointer to the destination buffer
*
*Exceptions:
*******************************************************************************/
/////////////////////////////////////////////////////////////////////////////////
/*说明:
1. __cdecl 是C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些参数由调用者清除,称为手动清栈。被调用函数不会要求调用者传递多少参数,调用者传递过多或者过少的参数,甚至完全不同的参数都不会产生编译阶段的错误。
2. 将n字节长的内容从一个内存地址复制到另一个地址;与memcpy不同的是它可以正确作用于两个存在重叠的地址。
3. 按照ANSI(American National Standards Institute)标准,不能对void指针进行算法操作,即不能对void指针进行如p++的操作,所以需要转换为具体的类型指针来操作,例如char *。(引用网友的结论)
4. size_t 类型定义在cstddef头文件中,该文件是C标准库的头文件stddef.h的C++版。它是一个与机器相关的unsigned类型,其大小足以保证存储内存中对象的大小。
*/
void * __cdecl smemmove (
void * dst,
constvoid * src,
size_t count
)
{
void * ret = dst;
#if defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC)
{
externvoid RtlMoveMemory(void *,constvoid *, size_t count );
RtlMoveMemory( dst, src, count );
}
#else /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
if (dst <= src || (char *)dst >= ((char *)src + count)) {
/*
* Non-Overlapping Buffers 没有重叠
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst +1;
src = (char *)src +1;
}
}
else {
/*
* Overlapping Buffers 重叠了
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count -1;
src = (char *)src + count -1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst -1;
src = (char *)src -1;
}
}
#endif /* defined (_M_MRX000) || defined (_M_ALPHA) || defined (_M_PPC) */
return(ret);
}
int main()
{
constchar src[50] ="blog.csdn.net/barry_yan";
char dst[50]="you are a good boy and she is a good girl !!!!";
cout<
smemmove(dst,src,strlen(src));
cout<
return0;
}
/////////////////////////////////////////////////////////////////////////////////
实验调试:
you are a good boy and she is a good girl !!!!
blog.csdn.net/barry_yanshe is a good girl !!!!