这两个函数用于拷贝字符串或者一段连续的内存,函数原型:

void * memcpy ( void * destination, const void * source, size_t num );
void * memmove ( void * destination, const void * source, size_t num );

这里有一点需要注意:num指的是需要拷贝的字节数,所以在将void*转型成实际的类型的时候一定要考虑重新计算拷贝的单元数

比如,转成WORD型,则实际需要拷贝的单元数位num / 2

参看glibc里面对于这两个函数的实现:
void* memmove(void* dest, const void* src, size_t len)
{
unsigned long int dstp = (long int) dest;
unsigned long int srcp = (long int) src; /* This test makes the forward copying code be used whenever possible.
Reduces the working set. */
if (dstp - srcp >= len) /* *Unsigned* compare! */
{
/* Copy from the beginning to the end. */ /* If there not too few bytes to copy, use word copy. */
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ); /* Copy whole pages from SRCP to DSTP by virtual address
manipulation, as much as possible. */ PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); /* Copy from SRCP to DSTP taking advantage of the known
alignment of DSTP. Number of bytes remaining is put
in the third argument, i.e. in LEN. This number may
vary from machine to machine. */ WORD_COPY_FWD (dstp, srcp, len, len); /* Fall out and copy the tail. */
} /* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_FWD (dstp, srcp, len);
}
else
{
/* Copy from the end to the beginning. */
srcp += len;
dstp += len; /* If there not too few bytes to copy, use word copy. */
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
len -= dstp % OPSIZ;
BYTE_COPY_BWD (dstp, srcp, dstp % OPSIZ); /* Copy from SRCP to DSTP taking advantage of the known
alignment of DSTP. Number of bytes remaining is put
in the third argument, i.e. in LEN. This number may
vary from machine to machine. */ WORD_COPY_BWD (dstp, srcp, len, len); /* Fall out and copy the tail. */
} /* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_BWD (dstp, srcp, len);
} return dest;
}

  有没有发现里面有一个不太“合适”的地方:

if (dstp - srcp >= len) /* *Unsigned* compare! */

如果dstp < srcp 呢?事实上C语言对这种行为早有定义:

A computation involving unsigned operands can never overflow,
   because a result that cannot be represented by the resulting
   unsigned integer type is reduced modulo the number that is
   one greater than the largest value that can be represented by the resulting type.

简单说,unsigned(0) - unsigned(1) = -1 + UINT_MAX + 1

也就是说,memmove所要处理的重叠分两种:1、dst在src前面2、dst在src后面
如果dst 在src前面而又重叠,只需前向复制就没有问题,此时dst - src 本来应该是负数的,但由于是unsignd 类型,所以相当于加上UINTMAX + 1了,肯定比len大,按函数中第一种情形处理了
如果dst 在src后面而有重叠,这时需要反向复制,也就是第二种情形。

void *
memcpy (void* dst, const void* src, size_t len)
{
unsigned long int dstp = (long int) dst;
unsigned long int srcp = (long int) src; /* Copy from the beginning to the end. */ /* If there not too few bytes to copy, use word copy. */
if (len >= OP_T_THRES)
{
/* Copy just a few bytes to make DSTP aligned. */
len -= (-dstp) % OPSIZ;
BYTE_COPY_FWD (dstp, srcp, (-dstp) % OPSIZ); /* Copy whole pages from SRCP to DSTP by virtual address manipulation,
as much as possible. */ PAGE_COPY_FWD_MAYBE (dstp, srcp, len, len); /* Copy from SRCP to DSTP taking advantage of the known alignment of
DSTP. Number of bytes remaining is put in the third argument,
i.e. in LEN. This number may vary from machine to machine. */ WORD_COPY_FWD (dstp, srcp, len, len); /* Fall out and copy the tail. */
} /* There are just a few bytes to copy. Use byte memory operations. */
BYTE_COPY_FWD (dstp, srcp, len); return dst;
}

  可以发现memcpy比memmove少了检查destp - srcp >= len的部分,这带来了memmove的优越之处:可以处理目的地址于源地址重叠的情形!

系统内置的memmove和memcpy是利用汇编优化的,当自己实现的时候,可以这么写:

void* n_memmove(void *dst, const void *src, size_t len) {
char* dstp = (char*)dst;
char* srcp = (char*)src;
if (len == 0) return dst;
assert(dst != NULL && src != NULL);
if ((unsigned int)dst - (unsigned int)src >= len) {
//byte_copy_forward(dstp, srcp, len);
for (int i = 0; i < len; i++)
dstp[i] = srcp[i];
}
else {
//copy from the end to the beginning
//byte_copy_bwd(dstp, srcp, len);
for (int i = len - 1; i >= 0; i--)
dstp[i] = srcp[i];
}
return dst;
} void* n_memcpy(void* dst, const void* src, size_t len) {
char* dstp = (char*)dst;
char* srcp = (char*)src;
assert(dst != NULL && src != NULL);
if (len == 0) return dst;
//byte_copy_forwar(dstp, srcp, len);
for (int i = 0; i < len; i++)
dstp[i] = srcp[i];
return dst;
}

  

C/C++ memmove 和 memcpy的更多相关文章

  1. memmove和memcpy

    1.memmove 函数原型:void *memmove(void *dest, const void *source, size_t count) 返回值说明:返回指向dest的void *指针 参 ...

  2. memmove和memcpy 以及strcmp strcpy几个库函数的实现

    memmove和memcpy 1.memmove 函数原型:void *memmove(void *dest, const void *source, size_t count) 返回值说明:返回指向 ...

  3. memmove、memcpy和memccpy简介

    memmove.memcpy和memccpy三个函数都是内存的拷贝,从一个缓冲区拷贝到另一个缓冲区.memmove(void *dest,void*src,int count)memcpy(void ...

  4. memmove、memcpy、strcpy、memset的实现

    memmove.memcpy.strcpy.memset 原型为: void *memmove( void* dest, const void* src, size_t count ); char*  ...

  5. [转]memmove、memcpy和memccpy

    原文地址:http://www.cppblog.com/kang/archive/2009/04/05/78984.html 在原文基础上进行了一些小修改~ memmove.memcpy和memccp ...

  6. memmove和memcpy函数的区别及实现

    一.memmove()和memcpy()函数和strcpy()函数的区别: (1)使用的类型不同,strcpy()函数只对字符串进行操作:memmove()和memcpy()函数对所有类型都适用,为内 ...

  7. memmove 和 memcpy的区别

    memcpy和memmove()都是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下:void *memcpy(void *dst, const void * ...

  8. memmove 和 memcpy的区别以及处理内存重叠问题

    区别: memcpy和memmove()都是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下: void *memcpy(void *dst, const v ...

  9. 内存操作函数memmove,memcpy,memset

    通过字符串的学习,我们知道字符串操作函数的操作对象是字符串,并且它的结束标志是结束符\0,当然这个说的是不 受限制的字符串函数.然而当我们想要将一段内存的数据复制到另一块内存时,我们不能使用字符串操作 ...

随机推荐

  1. redis订阅发布

    一.简介 Pub/Sub 从字面上理解就是发布(Publish)与订阅(Subscribe),在Redis中,你可以设定对某一个key值进行消息发布及消息订阅,当一个key值上进行了消息发布后,所有订 ...

  2. 企业用户选择Java多于.NET的 5个原因

    .NET 和 Java 是当今社会最受欢迎的两种编程语言, 长期的发展和强大的功能使他们足以在编程界立足. 十余年的争论也没得到结果的一个话题就是——他们哪个更好一些? 今天, 我们不再去讨论JAVA ...

  3. vs2010 ctrl+F5闪退解决方法

    设置项目的属性页中的“配置属性”->“链接器”->“系统”->“子系统”->“控制台”(即增加“/SUBSYSTEM:CONSOLE”链接选项)

  4. mouseover 和 hover 方法

    mouseover 和 hover 方法的不同?jquery   hover包括了鼠标移到对象上,同时鼠标再移出对象的过程,相应的子类也被选中. mouseover是鼠标经过对象时,不包含他的子类同时 ...

  5. MATLAB画图

    画图代码 clear % http://www.peteryu.ca/tutorials/matlab/visualize_decision_boundaries % load RankData % ...

  6. __attribute__特性介绍以及变量和函数特定布局设置

    ARM的MDK编译__attribute__介绍:http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0348bc/Ciafc ...

  7. MySQL 命令杂记

    mysql> show processlist; 如果是root帐号,你能看到所有用户的当前连接.如果是其它普通帐号,只能看到自己占用的连接.show processlist;只列出前100条, ...

  8. Octopus系列之重新规范了模板结构,大家快来看啊

    模板结构我是这样来设计的:大家请看,下面四个关键字 frontend[前段]backend[后端]member[会员]widget[部件] 前段页面包括 _Frontend_header_1.html ...

  9. 当进入log文件后就卡机

     问题:一个目录打开,终端就卡死不动了 Ctrl+c也没用,cat一样没用? 解决办法:用的时间或用的数量删除(时间已经否决掉) ls -t |tail -1000 |xargs rm 原因: log ...

  10. springmvc 配置直接访问页面

    <mvc:view-controller path="/" view-name="/home"/> 在mvc中配置,访问路径就可以了