【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
}
相关推荐
CoovallyAIHub15 小时前
Moonshine:比 Whisper 快 100 倍的端侧语音识别神器,Star 6.6K!
深度学习·算法·计算机视觉
CoovallyAIHub16 小时前
速度暴涨10倍、成本暴降6倍!Mercury 2用扩散取代自回归,重新定义LLM推理速度
深度学习·算法·计算机视觉
CoovallyAIHub16 小时前
实时视觉AI智能体框架来了!Vision Agents 狂揽7K Star,延迟低至30ms,YOLO+Gemini实时联动!
算法·架构·github
CoovallyAIHub16 小时前
开源:YOLO最强对手?D-FINE目标检测与实例分割框架深度解析
人工智能·算法·github
CoovallyAIHub17 小时前
OpenClaw:从“19万星标”到“行业封杀”,这只“赛博龙虾”究竟触动了谁的神经?
算法·架构·github
天朝八阿哥17 小时前
使用Docker+vscode搭建离线的go开发调试环境
后端·docker·visual studio code
刀法如飞17 小时前
程序员必须知道的核心算法思想
算法·编程开发·算法思想
徐小夕18 小时前
pxcharts Ultra V2.3更新:多维表一键导出 PDF,渲染兼容性拉满!
vue.js·算法·github
CoovallyAIHub19 小时前
OpenClaw一脚踩碎传统CV?机器终于不再只是看世界
深度学习·算法·计算机视觉