C语⾔内存函数

内存函数

1.memcpy使用和模拟实现

memcpy的官方详解

c 复制代码
void* memcpy(void* destination, const void* source, size_t num);
  1. 函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。
  2. 这个函数在遇到'\0'的时候并不会停下来。
  3. 如果source和destination有任何重叠,复制的结果都是未定义的。
c 复制代码
#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);//20字节 = 5 个整型

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);//1 2 3 4 5 0 0 0 0 0
	}

	return 0;
}

对于重叠的内存,交给memmove来处理。

c 复制代码
#include <stdio.h>
#include <string.h>
#include <assert.h>

void* my_memcpy(void* dest, const void* src, size_t num)
{
	void* ret = dest;
	assert(dest && src);

	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);//20字节 = 5 个整型

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);//1 2 3 4 5 0 0 0 0 0
	}

	return 0;
}

2.memmove使用和模拟实现

memmove的官方详解

c 复制代码
void* memmove(void* destination, const void* source, size_t num);
  1. 与memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  2. 如果源空间和目标空间出现重叠,就得使用memmove函数处理。
c 复制代码
#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);

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}

	return 0;
}

输出的结果:

c 复制代码
1 2 1 2 3 4 5 8 9 10

memmove的模拟实现:

c 复制代码
#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 || (char*)dest >= (char*)src + num)
	{
		//前 -> 后
		while (num--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		//后 -> 前
		dest = (char*)dest + num - 1;
		src = (char*)src + num - 1;

		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 };
	my_memmove(arr1 + 2, arr1, 20);

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}

	return 0;
}

3.memset的使用

memset的官方详解

c 复制代码
void* memset(void* ptr, int value, size_t num);

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

c 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
	char str[] = "hello world";
	memset(str, 'x', 5);
	printf(str);

	return 0;
}

输出的结果:

c 复制代码
xxxxx world

4.memcmp函数的使用

memcmp的官方详解

c 复制代码
int memcmp(const void* ptr1, const void* ptr2, size_t num);
  • 比较从ptr1和ptr2指针指向的位置开始,向后的num个字节。
  • 返回值如下:
c 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
	char str1[] = "abcdef11";
	char str2[] = "acvdef22";
	int n = memcmp(str1, str2, sizeof(str1));

	if (n > 0)
		printf("'%s' is greater than '%s'.\n", str1, str2);
	else if(n < 0)
		printf("'%s' is less than '%s'.\n", str1, str2);
	else
		printf("'%s' is the same as '%s'.\n", str1, str2);

	return 0;
}
相关推荐
ben9518chen4 小时前
嵌入式Linux C语言程序设计九
linux·c语言
下午见。5 小时前
C语言结构体入门:定义、访问与传参全解析
c语言·笔记·学习
cs麦子6 小时前
C语言--函数(function)
c语言·开发语言
极智-9966 小时前
C语言编译器?【图文详解】Visual Studio安装配置编译运行C语言?C语言编译器下载安装?
c语言·visual studio·c语言编译器·c代码运行·visual studio下载·c编程·c语言下载
是苏浙6 小时前
零基础入门C语言之C语言实现数据结构之顺序表
c语言·开发语言·数据结构
代码雕刻家6 小时前
C语言中关于类型转换不匹配的解决方案
c语言·开发语言·算法
许长安8 小时前
c/c++ static关键字详解
c语言·c++·经验分享·笔记
逐步前行9 小时前
C数据结构--排序算法
c语言·数据结构·排序算法
许长安12 小时前
C/C++中的extern关键字详解
c语言·开发语言·c++·经验分享·笔记
奔跑吧邓邓子12 小时前
【C语言实战(71)】C语言进阶:树与图的奇妙数据之旅
c语言···开发实战