C/C++ memmove 和 memcpy

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

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;
}

  

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 (dstp - srcp >= 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;
}

  

时间: 2024-08-06 07:34:37

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

[转]memmove、memcpy和memccpy

原文地址:http://www.cppblog.com/kang/archive/2009/04/05/78984.html 在原文基础上进行了一些小修改~ memmove.memcpy和memccpy三个函数都是内存的拷贝,从一个缓冲区拷贝到另一个缓冲区. memmove(void *dest,void*src,int count) memcpy(void *dest,void *src,int count) memccpy(void*dest,void*src,int ch,int coun

memmove和memcpy

1.memmove 函数原型:void *memmove(void *dest, const void *source, size_t count) 返回值说明:返回指向dest的void *指针 参数说明:dest,source分别为目标串和源串的首地址.count为要移动的字符的个数 函数说明:memmove用于从source拷贝count个字符到dest,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中. 2.memcpy 函数原型:v

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

memmove和memcpy 1.memmove 函数原型:void *memmove(void *dest, const void *source, size_t count) 返回值说明:返回指向dest的void *指针 参数说明:dest,source分别为目标串和源串的首地址.count为要移动的字符的个数 函数说明:memmove用于从source拷贝count个字符到dest,如果目标区域和源区域有重叠的话,memmove能够保证源串在被覆盖之前将重叠区域的字节拷贝到目标区域中.

memmove 和 memcpy的区别

memcpy和memmove()都是C语言中的库函数,在头文件string.h中,作用是拷贝一定长度的内存的内容,原型分别如下:void *memcpy(void *dst, const void *src, size_t count); void *memmove(void *dst, const void *src, size_t count); 他们的作用是一样的,唯一的区别是,当内存发生局部重叠的时候,memmove保证拷贝的结果是正确的,memcpy不保证拷贝的结果的正确. 第一种情况

C/C++ memmove与memcpy的区别及实现

1.与字符串函数strcpy区别: memcpy与memmove都是对内存进行拷贝可以拷贝任何内容,而strcpy仅是对字符串进行操作. memcpy与memmove拷贝多少是通过其第三个参数进行控制而strcpy是当拷贝至'\0'停止. 2.函数说明: memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝N个字节到目标dst所指的内存地址的起始位置中. memmove函数的功能同memcpy基本一致,但是当src区域和dst内存区域重叠时,memcpy可能会出现错误,而memmo

内存操作函数memmove,memcpy,memset

通过字符串的学习,我们知道字符串操作函数的操作对象是字符串,并且它的结束标志是结束符\0,当然这个说的是不 受限制的字符串函数.然而当我们想要将一段内存的数据复制到另一块内存时,我们不能使用字符串操作函数,当然也 不能一个一个复制,所以就引入了内存操作函数. memcpy函数原型: void *memcpy(void *dst, const void *src, size_t size);将一块内存的数据复制到另一块内存.当两块内存重叠              的时候,memcpy就不能保证拷

memmove and &nbsp; memcpy

2016年2月9日       正月初二          晴 以前上课时候老师提到过memcpy memove让大家下去自己尝试着写出代码来,后来不知如何给忘了,今天复习笔记的时候看见了这两个函数,所以花了点时间把它给搞懂. memcpy 和memove都是c语言中的库函数,在头文件string.h中作用是拷贝一定长度内存的内容,原型分别如下: void *my_memcpy(void *dest,void *src,size_t count) void* my_memmove(void *d

memcpy 与 memmove

memcpy:C和C++当中使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中. 函数原型 void* memcpy(void* dest, const void* src, size_t count) 函数返回指向dest的指针: mencpy与memmove的区别:根据源代码编写自己的Mencpy 与Memmove #include <iostream> using namespace std; #incl

memcpy、memmove、memset及strcpy函数实现和理解

memcpy.memmove.memset及strcpy函数实现和理解 关于memcpy memcpy是C和C++ 中的内存拷贝函数,在C中所需的头文件是#include<string.h>, 在C++中需要包含的头文件是#include其函数原型如下: void *memcpy(void *dest, const void *src, size_t n); 其功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中. 库函数中的memcpy不能处理sr