[转]memmove函数
【FROM MSDN && 百科】
原型: void *memmove( void* dest, const void* src, size_tcount );
#include<string.h>
由src所指内存区域复制count个字节到dest所指内存区域。
src和dest所指内存区域可以重叠,但复制后dest内容会被更改。函数返回指向dest的指针。
Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
memmove的处理措施:
(1)当源内存的首地址等于目标内存的首地址时,不进行任何拷贝
(2)当源内存的首地址大于目标内存的首地址时,实行正向拷贝
(3)当源内存的首地址小于目标内存的首地址时,实行反向拷贝
//#define FIRST_DEMO
//#define SECOND_DEMO
#define MYMEMMOVE
#ifdef FIRST_DEMO
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(void)
{
char s[]="Golden Global View";
memmove(s,s+7,strlen(s)+1-7);//+1是取'\0',可以去掉看下结果
printf("%s\n",s);
getch();
return 0;
}
#elif defined SECOND_DEMO
#include <stdio.h>
#include <conio.h>
#include <string.h>
int main(void)
{
char str1[7]="aabbcc";
printf( "The string: %s\n", str1 );
memcpy(str1+2,str1,4);
printf( "New string: %s\n", str1 );
memmove(str1+2,str1,4);
printf( "New string: %s\n", str1 );
getch();
return 0;
}
/*output:
The string: aabbcc
New string: aaaabb
New string: aaaaaa
*/
#elif defined MYMEMMOVE
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <assert.h>
void *mymemmove(void *dest,const void *src,size_t coount);
int main(void)
{
char str1[7]="aabbcc";
mymemmove(str1+2,str1,4);
puts(str1);
getch();
return 0;
}
void *mymemmove(void *dest,const void *src,size_t count)
{
char *ret=(char *)dest;
char *dest_t=dest;
char *src_t=(char *)src;
assert( NULL !=src && NULL !=dest); if (dest_t<=src_t || dest_t>=src_t+count)
{
while(count--)
{
*dest_t++ = *src_t++;
}
}
else
{
dest_t+=count-1;
src_t+=count-1;
while(count--)
{
*dest_t--=*src_t--;
}
}
return ret;
}
#endif
[转]memmove函数的更多相关文章
- memmove函数
写一个函数,完成内存之间的拷贝 void* mymemcpy( void *dest, const void *src, size_t count ) { char* pdest = static_c ...
- 实现memmove函数
分析:memmove函数是<string.h>的标准函数,其作用是把从source开始的num个字符拷贝到destination.最简单的方法是直接复制,但是由于它们可能存在内存的重叠区, ...
- strcpy函数;memcpy函数;memmove函数
strcpy函数实现: char* strcpy(char* des,const char* source) { char* r=des; assert((des != NULL) && ...
- 面试题之实现系统函数系列一:实现memmove函数
编译环境 本系列文章所提供的算法均在以下环境下编译通过. [算法编译环境]Federa 8,linux 2.6.35.6-45.fc14.i686 [处理器] Intel(R) Core(TM)2 Q ...
- 【C语言】模拟实现memmove函数(考虑内存重叠)
//模拟实现memmove函数(考虑内存重叠) #include <stdio.h> #include <assert.h> #include <string.h> ...
- C语言memcpy()函数和memmove()函数
C语言memcpy()函数和memmove()函数 关于 memcpy() 函数,请先看链接. memcpy() 函数和 memmove() 函数的函数原型如下: void* memcpy(void ...
- 一些关于memcpy memmove函数的区别,和模拟实现
memcpy: 它是c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中. 函数原型:void* memcp ...
- strcpy和memcpy,memmove函数的区别
strcpy和memcpy的区别 strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制 ...
- C语言memmove()函数: 复制内存内容(可以重叠的内存块)
头文件:#include <string.h> memmove() 用来复制内存内容,其原型为: void * memmove(void *dest, const void *src, s ...
随机推荐
- Unity NGUI 创建简单的按钮
Unity版本:4.5.1 NGUI版本:3.6.5 注意NGUI版本,网上的大部分教程都是2.x版本的,在步骤上面略有不同,此文适合初学者. 示例: 通过NGUI创建一个背景和按钮. 1.首先创建一 ...
- bzoj1221
网络流与线性规划24题中的餐巾计划吧明显要拆点吧,把每一天拆成2个点,i,i+n起点 终点 容量 费用 s i inf c 每天都可以购买新毛巾 i ...
- Qt入门(14)——父窗口部件和子窗口部件
这个例子演示了如何创建一个父窗口部件和子窗口部件.我们下面使用一个单一的父窗口部件和一个独立的子窗口部件编写界面. #include <qvbox.h>我们添加了一个头文件qvbox ...
- Qt入门(13)——Qt的调用退出
如果我们创建了一个窗口,接下来使这个应用程序在用户让它退出的时候退出. #include <qfont.h>因为这个程序使用了QFont,所以它需要包含qfont.h.Qt的字体提取和X中 ...
- unicode编码、字符的转换和得到汉字的区位码
一:unicode编码.字符的转换截图 二:unicode编码.字符的转换代码 using System; using System.Collections.Generic; using System ...
- 使用vs2010编译 Python \ SIP \ PyQt4
(1)先使用vs2010编译 Python http://www.cnblogs.com/fortwo/archive/2013/04/16/3023871.html 注意,若编译的为debug版的P ...
- 动态规划——J 括号配对问题
J - 括号匹配 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- NHibernate遇到的问题集 持续更新。
问题1: “NHibernate.TypeMismatchException”类型的异常在 NHibernate.dll 中发生,但未在用户代码中进行处理 其他信息: Provided id of t ...
- Hat's Fibonacci(大数问题)
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;int a[ ...
- Clouds
1.Eucalyptus: Eucalyptus is a Linux-based software architecture that implements scalable private and ...