memmove()与memcpy()的区别与实现
2013-11-29 13:04
393 查看
equalsnull
[cpp] view
plaincopy
// MemMove.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
[cpp] view
plaincopy
void* memmove(void* dest, const void* src, size_t n)
{
if (n <= 0)
{
cout << "Invalid count number." << endl;
}
if (dest == NULL || src == NULL)
{
cout << "The dest or src address is null." << endl;
}
if (dest == src)
{
cout << "The dest equals src." << endl;
}
if ((char*)dest <= (char*)src || (char*)dest >= (char*)src + n)
{//对应图中2,3,4三种情况,赋值过程中,src中未使用的不会被覆盖
char* de = (char*)dest;
const char* sr = (const char*)src;
while (n--)
{
*de++ = *sr++;
}
}
else
{//对应图中的第一种情况
char* de = (char*)dest + n -1;
const char* sr = (const char*)src + n - 1;
while (n--)
{
*de-- = *sr--;
}
}
return dest;
}
[cpp] view
plaincopy
void* memmcpy(void* dest, const void* src, size_t n)
{
if (n <= 0)
{
cout << "Invalid count number." << endl;
}
if (dest == NULL || src == NULL)
{
cout << "The dest or src address is null." << endl;
}
if (dest == src)
{
cout << "The dest equals src." << endl;
}
char* de = (char*)dest;
const char* sr = (const char*)src;
while (n--)
{
*de++ = *sr++;
}
return dest;
}
[cpp] view
plaincopy
[cpp] view
plaincopy
[cpp] view
plaincopy
int _tmain(int argc, _TCHAR* argv[])
{
char* p = "hello,world";
char dest[12] = {0};
char *q = (char*)memmove(dest,p,5);
cout << dest << endl;
cout << q << endl;
return 0;
}
[cpp] view
plaincopy
// MemMove.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
[cpp] view
plaincopy
void* memmove(void* dest, const void* src, size_t n)
{
if (n <= 0)
{
cout << "Invalid count number." << endl;
}
if (dest == NULL || src == NULL)
{
cout << "The dest or src address is null." << endl;
}
if (dest == src)
{
cout << "The dest equals src." << endl;
}
if ((char*)dest <= (char*)src || (char*)dest >= (char*)src + n)
{//对应图中2,3,4三种情况,赋值过程中,src中未使用的不会被覆盖
char* de = (char*)dest;
const char* sr = (const char*)src;
while (n--)
{
*de++ = *sr++;
}
}
else
{//对应图中的第一种情况
char* de = (char*)dest + n -1;
const char* sr = (const char*)src + n - 1;
while (n--)
{
*de-- = *sr--;
}
}
return dest;
}
[cpp] view
plaincopy
void* memmcpy(void* dest, const void* src, size_t n)
{
if (n <= 0)
{
cout << "Invalid count number." << endl;
}
if (dest == NULL || src == NULL)
{
cout << "The dest or src address is null." << endl;
}
if (dest == src)
{
cout << "The dest equals src." << endl;
}
char* de = (char*)dest;
const char* sr = (const char*)src;
while (n--)
{
*de++ = *sr++;
}
return dest;
}
[cpp] view
plaincopy
[cpp] view
plaincopy
[cpp] view
plaincopy
int _tmain(int argc, _TCHAR* argv[])
{
char* p = "hello,world";
char dest[12] = {0};
char *q = (char*)memmove(dest,p,5);
cout << dest << endl;
cout << q << endl;
return 0;
}
相关文章推荐
- memcpy和memmove区别和实现
- linux内核中memcpy和memmove函数的区别和实现
- memcpy和memmove的区别与实现
- linux内核中memcpy和memmove函数的区别和实现
- memcpy,memmove函数实现及区别
- memset ,memcpy ,memmove,strcpy 的根本区别 与实现
- strcpy、memcpy和memmove的区别和实现
- memcpy与memmove区别和实现
- memcpy和memmove() 实现和区别
- memcpy、memmove、strcpy函数实现源码及区别
- memcpy和memmove的区别与实现
- linux内核中memcpy和memmove函数的区别和实现
- memcpy和memmove函数的区别和实现
- 模拟实现memcpy、memmove函数以及二者的区别
- memcpy和memmove的区别与实现
- 常见内存拷贝函数:memcpy()、memmove()、strcpy()的实现及区别
- Linux内核中memcpy和memmove函数的区别和实现
- strcpy、memcpy、memmove的区别及实现
- strcpy、memcpy和memmove的区别和实现