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;
}

输出结果:

相关推荐
papership4 分钟前
【入门级-C++程序设计:11、指针与引用-引 用】
c语言·开发语言·c++·青少年编程
胖咕噜的稞达鸭3 小时前
数据结构---关于复杂度的基础解析与梳理
c语言·数据结构·算法·leetcode
范纹杉想快点毕业4 小时前
《嵌入式 C 语言编码规范与工程实践个人笔记》参考华为C语言规范标准
服务器·c语言·stm32·单片机·华为·fpga开发·51单片机
長琹5 小时前
9、C 语言内存管理知识点总结
linux·c语言
阿熊不凶7 小时前
c语言中堆和栈的区别
java·c语言·jvm
奶黄小甜包16 小时前
C语言零基础第16讲:内存函数
c语言·笔记·学习
源远流长jerry1 天前
OpenHarmony概述与使用
c语言·c++·鸿蒙系统
艾莉丝努力练剑1 天前
深入详解C语言的循环结构:while循环、do-while循环、for循环,结合实例,讲透C语言的循环结构
c语言·开发语言·c++·学习
晨非辰1 天前
#C语言——学习攻略:自定义类型路线--结构体--结构体类型,结构体变量的创建和初始化,结构体内存对齐,结构体传参,结构体实现位段
c语言·开发语言·经验分享·学习·其他·学习方法·visual studio
·白小白1 天前
【数据结构】——栈(Stack)的原理与实现
c语言·开发语言·数据结构