【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
}
相关推荐
胖咕噜的稞达鸭17 分钟前
C++中的父继子承:继承方式实现栈及同名隐藏和函数重载的本质区别, 派生类的4个默认成员函数
java·c语言·开发语言·数据结构·c++·redis·算法
笑口常开xpr24 分钟前
【C++】模板 - - - 泛型编程的魔法模具,一键生成各类代码
开发语言·数据结构·c++·算法
一碗绿豆汤41 分钟前
c语言-运算符
c语言
AI视觉网奇1 小时前
pyqt 触摸屏监听
开发语言·python·pyqt
香菜+1 小时前
python脚本加密之pyarmor
开发语言·python
数据知道1 小时前
Go基础:一文掌握Go语言泛型的使用
开发语言·后端·golang·go语言
啃啃大瓜1 小时前
常用库函数
开发语言·python
IT小番茄1 小时前
Kubernetes云平台管理实战:自动加载到负载均衡(七)
算法
笑口常开xpr1 小时前
【C++继承】深入浅出C++继承机制
开发语言·数据结构·c++·算法
你不是我我2 小时前
【Java开发日记】请介绍类加载过程,什么是双亲委派模型?
java·开发语言