【C语言】_内存拷贝函数memcpy与memmove

目录

[1. memcpy](#1. memcpy)

[1.1 函数声明及功能](#1.1 函数声明及功能)

[1.2 使用示例](#1.2 使用示例)

[1.3 模拟实现](#1.3 模拟实现)

[2. memmove](#2. memmove)

[2.1 函数声明与功能](#2.1 函数声明与功能)

[2.2 使用示例](#2.2 使用示例)

[2.3 模拟实现](#2.3 模拟实现)


1. memcpy

在专栏前文已介绍字符串拷贝函数strcpy,但其仅能实现字符串的拷贝。若需对其他类型如整型、浮点型、结构体等变量进行拷贝,则无法再使用strcpy实现。

memcpy函数针对内存块实现拷贝;

1.1 函数声明及功能

cpp 复制代码
void * memcpy ( void * destination, const void * source, size_t num );

memcpy函数功能:

将source位置开始向后num个字节的数据复制到destination指向的内存位置

返回值为拷贝的目标空间的起始地址;

注:1、memcpy在遇到\0时并不会停止;

2、如果source和destination有任何重叠,memcpu的行为是未定义的,可能导致数据损坏

3、memcpy虽然为内存拷贝函数,但使用时所需包含的头文件为string.h

1.2 使用示例

cpp 复制代码
#include<stdio.h>
#include<string.h>
int main() {
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	memcpy(arr2, arr1,5*4);
	for (int i =0; i < 20; i++) {
		printf("%d ", arr2[i]);
	}
	return 0;
}

运行结果如下:

1.3 模拟实现

cpp 复制代码
#include<stdio.h>
#include<string.h>
#include<assert.h>
void* my_memcpy(void* dest, const void* src, size_t num) {
	assert(dest && src);
	int i = 0;
	void* ret = dest;
	// num单位为字节(而非元素)
	while (num--) {
		// 逐字节拷贝
		*((char*)dest) = *((char*)src);
		src = (char*)src + 1;
		dest = (char*)dest + 1;
	}
	// 返回拷贝的目标空间的起始地址
	return ret;
}
int main() {
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	memcpy(arr2, arr1,7*4);
	for (int i =0; i < 20; i++) {
		printf("%d ", arr2[i]);
	}
	return 0;
}

注:对于src = (char*)src + 1,不可写为src++,src为void*类型,不可直接进行加减运算;

但可写为((char*)src)++,表示强转为char*类型后再自增;

2. memmove

对于memcpy,它只负责不重叠空间的内存拷贝工作,在注2中已经提到;

若需实现拷贝的源和目标内存区域有重合部分的内容拷贝,则需使用memmove函数

2.1 函数声明与功能

cpp 复制代码
void * memmove ( void * destination, const void * source, size_t num );

注:对于调用者,相较于memcpy,memmove并未增加额外的参数。其可以实现重叠内存区域的拷贝的逻辑,在模拟实现部分进行说明;

2.2 使用示例

cpp 复制代码
#include<stdio.h>
#include<string.h>
int main() {
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	// 将1、2、3、4、5拷贝至3、4、5、6、7位置处
	memmove(arr1 + 2, arr1, 5 *sizeof(int));
	for (int i = 0; i < 10; i++) {
		printf("%d ", arr1[i]);
	}
	return 0;
}

运行结果如下:

2.3 模拟实现

memmove可以处理源内存区域和目标区域有重叠的情况,模式实现具体实现逻辑有两种:

(1)额外再开辟一个数组作为临时缓冲区,先将源内存区域的数据复制到一个临时缓冲区,再从临时缓冲区复制到目标内存区域,确保数据不会被覆盖;(两次拷贝实现,此处不再)

(2)分情况从前向后或从后向前依次将源内存空间数据拷贝至目标内存空间:

情况1:

情况2:

情况3:

现将情况2与3合并,故对src与dest分段如下:

当dest落在src之前时,从前向后依次拷贝;

当dest落在src之后时,从后向前依次拷贝;

cpp 复制代码
#include<stdio.h>
#include<string.h>
#include<assert.h>
void* my_memmove(void* dest, const void* src, size_t num) {
	assert(src && dest);
	void* ret = dest;
	if (dest < src) {
		// 从前向后依次拷贝
		while (num--) {
			*((char*)dest) = *((char*)src);
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else {
		// 从后向前依次拷贝
		while (num--) {
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	// 返回目标空间的起始地址
	return ret;
}
void PrintInt(int* arr, int sz) {
	for (int i = 0; i < sz; i++) {
		printf("%d ", *(arr + i));
	}
	printf("\n");
}
int main() {
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	// 将1、2、3、4、5拷贝至3、4、5、6、7位置处
	my_memmove(arr1 + 2, arr1, 5 * sizeof(int));
	PrintInt(arr1, sz);
	int arr2[] = { 1,2,3,4,5,6,7,8,9,10 };
	// 将3、4、5、6、7拷贝至1、2、3、4、5位置处
	my_memmove(arr2, arr2 + 2, 5 * sizeof(int));
	PrintInt(arr2, sz);
	return 0;
}

测试用例运行结果如下:

相关推荐
不当菜虚困几秒前
JAVA设计模式——(四)门面模式
java·开发语言·设计模式
ruyingcai666666几秒前
用python进行OCR识别
开发语言·python·ocr
m0Java门徒8 分钟前
面向对象编程核心:封装、继承、多态与 static 关键字深度解析
java·运维·开发语言·intellij-idea·idea
liuweidong080211 分钟前
【Pandas】pandas DataFrame radd
开发语言·python·pandas
农民也会写代码43 分钟前
dedecms织梦arclist标签noflag属性过滤多个参数
开发语言·数据库·sql·php·dedecms
内网渗透1 小时前
Python 虚拟环境管理:venv 与 conda 的选择与配置
开发语言·python·conda·虚拟环境·venv
embedded_w1 小时前
U8G2在PC端模拟(C语言版本)
c语言
每次的天空2 小时前
kotlin与MVVM结合使用总结(三)
开发语言·microsoft·kotlin
矛取矛求2 小时前
C++区别于C语言的提升用法(万字总结)
c语言·c++
keep intensify2 小时前
通讯录完善版本(详细讲解+源码)
c语言·开发语言·数据结构·算法