C语言 之 内存函数 memcpy、memmove函数的使用和模拟实现 memset、memcmp函数的使用

文章目录

首先 我们要明确下面这些函数之所以被称作内存函数,就是因为这些函数所针对的内容是内存空间

1.memcpy函数的使用和模拟实现

函数原型:

void * memcpy ( void * destination, const void * source, size_t num);
1.函数memcpy是从source的位置开始向后复制num个字节的数据到destinatio指向的内存位置。(注意此处的num是num个字节,不是num个元素)

2.这个函数在遇到 '\0' 的时候并不会停下来。

例子:

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

输出结果:

模拟实现:

复制代码
#include <stdio.h>
#include <string.h>
#include<assert.h>
void* my_memcpy(void* dst, const void* src, size_t count)
{
	void* ret = dst;  //用来存储初始地址 便于结果的返回
	assert(dst);
	assert(src);
	while (count--) //以count作为条件 即需要拷贝的字节数
	{ 
		*(char*)dst = *(char*)src;  //注意强转为(char*)类型  +1跳过一个字节
		dst = (char*)dst + 1;
		src = (char*)src + 1;
	}
	return ret;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memcpy(arr2, arr1, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

memcpy的使用会有一个内容重叠的问题,即目标的空间位置和源的空间位置在内存空间中有重叠,造成输出结果的不理想

具体可以点击此处查看这篇文章的讲解

2.memmove函数的使用和模拟实现

函数原型:

void * memmove ( void * destination, const void * source, size_t num);
1.和memcpy的差别就是memmove函数处理的源内存块和⽬标内存块是可以重叠的。即没有内存重叠的问题。

2.如果源空间和⽬标空间出现重叠,就得使⽤memmove函数处理

例子:

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

输出结果:

模拟实现:

为了解决内存重叠的问题,我们可以通过改变拷贝的方式来解决。

memcpy是默认从前往后拷贝的,所以一旦位置出现问题,即dst和src两个指针的位置前后出现问题,就会造成影响

所以为了解决这个问题我们就根据这两个指针的位置的前后不同来进行不同的处理,加上了从后往前拷贝这个方法,就能解决这个问题了。

如果不了解内存重叠,建议先去了解一下。

复制代码
#include <stdio.h>
#include <string.h>
#include<assert.h>

void* my_memmove(void* dst, const void* src, size_t count)
{
	assert(dst && src);
	void* ret = dst; //用来存储初始地址 便于结果的返回
	if (dst < src)
	{
		//从前往后拷贝
		while (count--)
		{
			*(char*)dst = *(char*)src;
			dst = (char*)dst + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		//从后往前拷贝
		while (count--)
		{
			*((char*)dst+count) = *((char*)src+count); // 通过+count 来到达后面的位置 
																				//并且count-- 这样就能做到从后往前拷贝
		}
	}

	return ret;
}

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

3.memset函数的使用

函数原型:

void * memset ( void * ptr, int value, size_t num );

++memset是用来设置内存的,将内存中的值以字节为单位设置成想要的内容。++

例子:

复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "hello fall";
	memset(str, 'x', 5);
	printf(str);
	return 0;
}

输出结果:

3.memcmp函数的使用

函数原型:

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

++比较从ptr1和ptr2指针指向的位置开始,向后的num个字节++

返回值:

这个函数和strcmp类似,比较的是ASCII码值,而相等则返回0,ptr1比ptr2大则返回>0的值

例子:

复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char buffer1[] = "abd";
	char buffer2[] = "abc";
	int tmp = memcmp(buffer1, buffer2, sizeof(buffer1));
	if (tmp > 0)
		printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
	else if (tmp < 0)
		printf("'%s' is less than '%s'.\n", buffer1, buffer2);
	else
		printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
	return 0;
}

输出结果:

相关推荐
森焱森17 分钟前
60 美元玩转 Li-Fi —— 开源 OpenVLC 平台入门(附 BeagleBone Black 驱动简单解析)
c语言·单片机·算法·架构·开源
千帐灯无此声37 分钟前
Linux 测开:日志分析 + 定位 Bug
linux·c语言·c++·bug
ChuHsiang2 小时前
【C语言学习】实现游戏——三子棋
c语言
L_autinue_Star4 小时前
手写vector容器:C++模板实战指南(从0到1掌握泛型编程)
java·c语言·开发语言·c++·学习·stl
怀旧,5 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
凤年徐6 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
学废了wuwu7 小时前
深度学习归一化方法维度参数详解(C/H/W/D完全解析)
c语言·人工智能·深度学习
无小道7 小时前
c++--typedef和#define的用法及区别
c语言·开发语言·汇编·c++
敲上瘾8 小时前
传输层协议UDP原理
linux·c语言·网络·网络协议·udp
小林C语言9 小时前
C语言 | 文件读写检测
c语言