memcpy和memmove
来源:互联网 发布:淘宝标签怎么设置 编辑:程序博客网 时间:2024/04/30 19:38
下面是一个memcpy的结构体的复制,有点类似于C++中调用类的拷贝构造函数,用一个对象去初始化另一个对象!
例:
#include "stdio.h"
#include "string.h"
void main(void)
{
struct student
{
char *name;
int age;
}stu1={"wangqiao",24};
struct student stu2;
memcpy(&stu2,&stu1,sizeof(stu1));
printf("stu2.name = %s/nstu2.age = %d/n",stu2.name,stu2.age);
getch();
}
;===========================================
memmove
原型:extern void *memmove(void *dest, const void *src, unsigned int count); 用法:#include <string.h> 功能:由src所指内存区域复制count个字节到dest所指内存区域。 说明:src和dest所指内存区域可以重叠,但复制后src内容会被更改。函数返回指向dest的指针。 举例: // memmove.c #include <syslib.h> #include <string.h> main() { char *s="Golden Global View"; clrscr(); memmove(s,s+7,strlen(s)-7); s[strlen(s)-7]=0; printf("%s",s); getchar(); return 0; } 相关函数:memccpy,memcpy,strcpy
;===============================================================================
区别:
代码:
;***
;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;
}
- memmove和memcpy
- memmove和memcpy
- memmove和memcpy研究
- memcpy和memmove 区别
- memcpy和memmove
- memmove和memcpy区别
- memmove和memcpy
- memmove和memcpy
- memmove和memcpy
- memcpy和memmove
- memcpy() memmove()和memccpy()
- memcpy() 和memmove()区别
- memmove和memcpy
- memmove和memcpy
- memmove 和memcpy
- memmove和memcpy
- memcpy()、 memmove()和memccpy()
- memmove和memcpy
- c/c++编程规范
- 数据字典
- Web 设计师的 50 个超便利工具
- 轻松使用 ffmpeg sdk 实现各种格式的rgb以及yuv raw
- 在Silverlight中运行基于XNA开发的游戏
- memcpy和memmove
- TreeView的子节点和父节点级联选中
- PHP 单元测试(PHPUnit)(1)
- C++读取mat数据(1)【转载】
- 由C++入门Objective-C——Objective C 的语言特性
- PHP 单元测试(PHPUnit)(2)
- Java正则表达式
- 什么是DEH?为什么要采用DEH控制?
- C++读取mat数据(2)【转帖】