【C语言】:内存函数

这里写目录标题

1、memcpy的使用和模拟

void * memcpy ( void * destination, const void * source, size_t num );

  • 将 num字节的值从源指向的位置直接复制到目标指向的内存块。

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

    • 如果source和destination有任何的重叠,复制的结果都是未定义的。

我们来举个例子:

csharp 复制代码
#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, 20);
	int i = 0;
	for (i = 0; i < 20; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;//1 2 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
}

memcpy函数的模拟实现:

csharp 复制代码
#include<stdio.h>
#include<string.h>
void* my_memcpy(void* dest,const void* src, size_t num)
{
	void* ret = dest;
	while (num--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	my_memcpy(arr2, arr1, 20);
	int i = 0;
	for (i = 0; i < 20; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;1 2 3 4 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
}

2、memmove的使用和模拟

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

  • 将 num字节的值从源指向的位置复制到目标指向的内存块。

    • 和memcpy的差别就是memmove函数处理的源内存块和⽬标内存块是可以重叠的。

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

举个例子:

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

memmove函数的模拟实现:

csharp 复制代码
#include<stdio.h>
#include<string.h>
#include<assert.h>
void* my_memmove(void* dest, const void* src, size_t num)
{
	void* ret = dest;
	assert(dest && src);
	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;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr1 + 2, arr1, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;//1 2 1 2 3 4 5 8 9 10
}

3、memset函数的使用

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

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

这里举个例实现一下:

csharp 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "hello world";
	memset(str, 'x', 6);
	printf("%s\n", str);//xxxxxxworld
	return 0;
}

4、memcmp 函数的使用

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

  • 比较两个内存块

    • 将 ptr1 指向的内存块的第一个 num 字节与 ptr2 指向的第一个 num 字节进行比较,如果它们都匹配,则返回 0,或者返回一个与 0 不同的值,表示如果它们不匹配,则哪个值更大。

返回值 表明
>0 在两个内存块中第一个字节在ptr1中的值大于ptr2中的值
=0 在两个内存块中第一个字节在ptr1中的值等于ptr2中的值
<0 在两个内存块中第一个字节在ptr1中的值小于ptr2中的值
举个例子实践一下:
csharp 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	int arr1[] = { 1,2,1,4,5,6 };
	int arr2[] = { 1,2,257 };
	int ret = memcmp(arr1, arr2, 10);
	printf("%d\n", ret);
	return 0;//-1
}
相关推荐
CoderYanger6 小时前
Java EE 进阶:13. Spring事务和事务传播机制(第二弹)
java·开发语言·数据库·spring boot·后端·spring·java-ee
星恒随风10 小时前
从0实现C++ AI大模型接入SDK(八):SessionManager会话管理——智能指针、原子操作、线程安全与懒加载机制
linux·服务器·开发语言·c++·人工智能
geats人山人海11 小时前
算法基础第六讲 图论
算法·深度优先·图论
yingjie11011 小时前
快榜GEO|官网 AI 可读性自查 12 项(附 Python 检测脚本)
开发语言·人工智能·python
Felven12 小时前
C. Make Them Equal
c语言·开发语言·前端
tryCbest12 小时前
Python 爬虫扩展篇(二):验证码的检测与人工介入——把“拦路虎”当作信号,而不是要征服的对手
开发语言·爬虫·python
风落青芜14 小时前
排序算法(四):归并排序、计数排序与算法总结
c语言·开发语言·数据结构·算法·排序算法
Resean022314 小时前
讲透Java(14)泛型的本质:类型擦除与桥接方法
java·开发语言
@陈小鱼14 小时前
基于InceptionTime网络的PPG无创血压估计算法研究
人工智能·python·算法·回归·开源·ppg·血压估计