C语言内存函数(memcpy及memove的了解与使用)

本文介绍了内存函数在C/C++中的重要性,包括memcpy、memmove、memset和memcmp的具体用法、语法示例以及它们之间的区别。作者还提供了内存函数的模拟实现代码以帮助理解。
摘要由CSDN通过智能技术生成

前言:

        前边我们了解了一些字符函数的使用以及它是如何实现的,但是我们会发现这些函数只可以针对字符串使用,对于其它类型的数据我们将无法使用这个函数的功能,那么我们今天就带着兄弟们了解一下何为内存函数,内存函数又可以实现哪些功能,它针对的类型又是什么

内存函数:

1. memcpy使⽤和模拟实现

2. memmove使⽤和模拟实现

3. memset函数的使⽤

4. memcmp函数的使⽤

1. memcpy函数:

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

语法:

void * memcpy ( void * destination, const void * source, size_t num );
(注意:此处的num代表的是num个字节数,并非一个元素)

举例:

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr1[20] = { 0 };
	memcpy(arr1, arr, 16);  
//此处的16代表着16个字节,而我们访问的数组是int型,所以此处拷贝的是4个元素
	for (int i = 0; i < 20; i++)
	{
		printf("%d ", arr1[i]);
	}

}

//执行结果为
1 2 3 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

memcpy的模拟实现():

memcpy函数模拟实现
#include <assert.h>   //assert函数的头文件
//由于们memcpy函数是可以实现多种数据的拷贝,所以此处传入时指针的类型为void* ,方便后续强制转换
void* in_memcpy(void* destination, const void* source, size_t num)  
{
	void* ret = destination;
	assert(destination && source);  //判断传入的两个指针是否为空
	while (num--)
	{
		*(char*)destination = *(char*)source;  //逐字节交换
		((char*)destination)++;
		((char*)source)++;
	}
	return ret;  //返回值
}
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr1[20] = { 0 };
	void* ret = in_memcpy(arr1, arr, 16);  //16为字节数
	for (int i = 0; i < 20; i++)
	{
		printf("%d ", arr1[i]);
	}
    return 0;
}

2. memove函数:

1. 和memcpy的差别就是memmove函数处理的源内存块和⽬标内存块是可以重叠的
2. 如果源空间和⽬标空间出现重叠,就得使⽤memmove函数处理

语法:

void * memmove ( void * destination, const void * source, size_t num );
举例:

#include <string.h>
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr1[20] = { 0 };
//从3开始拷贝三个元素到1往后三个元素的位置
	memmove(arr, arr+2, 12);  //12表示12个字节数 此处等于3个int元素
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
}

memmove函数的模拟实现:

#include <stdio.h>
#include <assert.h>
#include <string.h>
void* in_memove(void* destination, const void* source, size_t num)
{
	void* ret = destination;
	assert(destination && source);
//此处判断满足条件从前面拷贝,不满足从后边拷贝
	if (destination <= source || (char*)destination >= (char*)source + num)
	{
		while (num--)
		{
			*(char*)destination = *(char*)source;
			((char*)destination)++;
			((char*)source)++;
		}
	}
	else
	{
		destination = (char*)destination + num - 1;
		source = (char*)source + num - 1;
		while (num--)
		{
			*(char*)destination = *(char*)source;
			((char*)destination)--;
			((char*)source)--;
		}
	}
	return ret;
}
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr1[20] = { 0 };
	void* ret = in_memove(arr, arr+2, 12);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
}

//执行结果
3 4 5 4 5 6 7 8 9 0

3. memset函数

memset函数将内存中以字节为单位你想要设置的值设置为你想要的内容

语法:

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

举例:

#include <string.h>
int main()
{
	char arr[] = "L love china";
	memset(arr, 'x', 5);
	for (int i = 0; i < 10; i++)
	{
		printf("%c", arr[i]);
	}
}

//执行结果
xxxxxe china

4. memcmp函数:

语法:

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

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

(注:此处是逐字节比较,大于返回>0的数,小于返回<0的数,等于则=0)

举例:

#include <string.h>
int main()
{
	char arr[] = "abcdf";
	char arr1[] = "abced";
	int ret = memcmp(arr, arr1, 4);
	printf("%d\n", ret);
}

//执行结果(此时arr<arr1,所以返回小于0的数)
vs2022返回-1

(今日分享到此结束,有啥高见欢迎评论区留言,Thanks♪(・ω・)ノ!!!)

  • 19
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

糟糕喔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值