模拟实现C语言--memcpy函数和memmove函数

模拟实现C语言--memcpy函数和memmove函数

文章目录

  • 模拟实现C语言--memcpy函数和memmove函数
  • 一、memcpy函数和memmove函数
    • [1.1 memcpy函数是什么](#1.1 memcpy函数是什么)
    • [1.1 memmove函数是什么](#1.1 memmove函数是什么)
  • 二、使用示例
    • [2.1 从起始位置复制](#2.1 从起始位置复制)
    • [2.2 从任意位置复制](#2.2 从任意位置复制)
  • 三、模拟实现
    • [3.1 模拟实现1--memcpy函数](#3.1 模拟实现1--memcpy函数)
    • [3.2 针对缺点改进的模拟实现2--memmove函数](#3.2 针对缺点改进的模拟实现2--memmove函数)
      • [3.2.1 刨析原因](#3.2.1 刨析原因)
      • [3.2.2 改正方法](#3.2.2 改正方法)
      • [3.2.3 代码--模拟实现memmove函数](#3.2.3 代码--模拟实现memmove函数)
      • [3.2.4 memcpy函数和memmove函数平台问题](#3.2.4 memcpy函数和memmove函数平台问题)

一、memcpy函数和memmove函数

1.1 memcpy函数是什么

c 复制代码
void * memcpy ( void * destination, const void * source, size_t num );
  1. strcpy函数是字符串拷贝函数,只能拷贝字符串,而其他类型无法使用strcpy函数拷贝
  2. 而memcpy函数属于内存拷贝函数,可以拷贝其他类型。

1.1 memmove函数是什么

c 复制代码
void * memmove ( void* destination, const void * source, size_t num );
  • 和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

二、使用示例

  1. 函数memcpy从source的位置开始向后复制num个字节的数据到destination的内存位置。
  2. 这个函数在遇到 '\0' 的时候并不会停下来
  3. 如果source和destination有任何的重叠,复制的结果都是未定义的。

2.1 从起始位置复制

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

2.2 从任意位置复制

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

三、模拟实现

3.1 模拟实现1--memcpy函数

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string.h>
#include <assert.h>

void* my_memcpy(void* destination, const void* source, size_t num)
{
	void* ret = destination;
	assert(destination);
	assert(source);
	/*
	 * copy from lower addresses to higher addresses
	 */
	while (num--) 
	{
		*(char*)destination = *(char*)source;
		destination = (char*)destination + 1;
		source = (char*)source + 1;
	}
	return(ret);
}
int main()
{
	int i = 0;
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memcpy(arr2, arr1+2, 20);
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

3.2 针对缺点改进的模拟实现2--memmove函数

模拟实现1的代码有一个缺陷,就是不能进行自我拷贝

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string.h>
#include <assert.h>

void* my_memcpy(void* destination, const void* source, size_t num)
{
	void* ret = destination;
	assert(destination);
	assert(source);
	/*
	 * copy from lower addresses to higher addresses
	 */
	while (num--)
	{
		*(char*)destination = *(char*)source;
		destination = (char*)destination + 1;
		source = (char*)source + 1;
	}
	return(ret);
}
int main()
{
	int i = 0;
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memcpy(arr1+2, arr1, 20);
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

3.2.1 刨析原因

3.2.2 改正方法

  1. 当dst指针指向的地址在src指针指向位置的右边时,这种情况的赋值应该从后向前赋值,就是12345,先让5赋值在7的位置,依次循环
  2. 当dst指针指向的地址在src指针指向位置的右边时,这种情况的赋值应该从前向后赋值,34567,先将3赋值给1的位置,依次循环

3.2.3 代码--模拟实现memmove函数

c 复制代码
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include <string.h>
#include <assert.h>

void* my_memmove(void* destination, const void* source, size_t num)
{
	void* ret = destination;
	assert(destination);
	assert(source);
	if (destination < source)
	{
		//从前向后赋值
		while (num--)
		{
			*(char*)destination = *(char*)source;
			destination = (char*)destination + 1;
			source = (char*)source + 1;
		}
	}
	//从后向前赋值
	else
	{
		while (num--)
		{
			*((char*)destination+num)= *((char*)source+num);

		}
		
	}
	return ret;
}


int main()
{
	int i = 0;
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memmove(arr1+2, arr1, 20);
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

3.2.4 memcpy函数和memmove函数平台问题

目前在vs环境下,这两个函数基本没有区别,memcpy也可以解决内存重复的问题。别的平台可能还是会有这个问题

相关推荐
小巧的砖头25 分钟前
使用SVN+CruiseControl+ANT实现持续集成之一
学习·算法
人道领域28 分钟前
【LeetCode刷题日记】贪心算法理论与实战:455.分发饼干最优解
java·开发语言·数据结构·算法·leetcode·贪心算法
会周易的程序员43 分钟前
microLog 的 log_reader 架构解析:嵌入式日志检索引擎的设计之道
c++·物联网·架构·日志·iot·aiot
汉克老师1 小时前
GESP2026年6月认证C++七级( 第一部分选择题(8-15))精讲
c++·dfs·bfs·哈希·二分算法·gesp7级·网格dp
zh_xuan1 小时前
c++ Expected用法
开发语言·c++·expected
小O的算法实验室1 小时前
2026年ACM TCH,动态救护车路径优化:结合 K-means 聚类与多目标粒子群算法两阶段策略
算法
在书中成长1 小时前
HarmonyOS 小游戏《对战五子棋》开发第8篇 - GomokuEngine核心引擎设计(三):五子连珠判定算法
算法·华为·harmonyos
Let's Chat Coding1 小时前
MAC 消息认证码:同时验证来源和完整性
算法·哈希算法
cc.ChenLy3 小时前
算法从入门到精通实战指南
算法
王老师青少年编程10 小时前
2026年6月GESP真题及题解(C++一级):交税
c++·题解·真题·gesp·一级·2026年6月·交税