【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
}
相关推荐
zhangyao9403302 小时前
关于js导入Excel时,Excel的(年/月/日)日期是五位数字的问题。以及对Excel日期存在的错误的分析和处理。
开发语言·javascript·excel
骑驴看星星a2 小时前
【Three.js--manual script】4.光照
android·开发语言·javascript
2301_795167203 小时前
玩转Rust高级应用 如何避免对空指针做“解引用”操作,在C/C++ 里面就是未定义行为
c语言·c++·rust
星释3 小时前
Rust 练习册 :Leap与日期计算
开发语言·后端·rust
智驱力人工智能4 小时前
基于视觉分析的人脸联动使用手机检测系统 智能安全管理新突破 人脸与手机行为联动检测 多模态融合人脸与手机行为分析模型
算法·安全·目标检测·计算机视觉·智能手机·视觉检测·边缘计算
悟能不能悟5 小时前
java的java.sql.Date和java.util.Date的区别,应该怎么使用
java·开发语言
2301_764441335 小时前
水星热演化核幔耦合数值模拟
python·算法·数学建模
循环过三天5 小时前
3.4、Python-集合
开发语言·笔记·python·学习·算法
_院长大人_6 小时前
设计模式-工厂模式
java·开发语言·设计模式
MATLAB代码顾问6 小时前
MATLAB实现决策树数值预测
开发语言·决策树·matlab