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;
}
相关推荐
laocooon5238578862 小时前
win操作系统安装C++语言开发环境之一, vscode +MinGW ,流程
c语言
奔跑吧邓邓子2 小时前
解锁Vscode:C/C++环境配置超详细指南
c语言·c++·vscode·配置指南
小柯博客10 小时前
从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(十二)
c语言·stm32·单片机·嵌入式硬件·php·嵌入式
乄夜14 小时前
嵌入式面试高频(5)!!!C++语言(嵌入式八股文,嵌入式面经)
c语言·c++·单片机·嵌入式硬件·物联网·面试·职场和发展
乖乖是干饭王16 小时前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu
weixin_4786897617 小时前
C++ 对 C 的兼容性
java·c语言·c++
待什么青丝17 小时前
【TMS570LC4357】之相关驱动开发学习记录2
c语言·arm开发·驱动开发·单片机·学习
小柯博客17 小时前
从零开始打造 OpenSTLinux 6.6 Yocto 系统(基于STM32CubeMX)(九)
c语言·stm32·单片机·嵌入式硬件·物联网
CodeWithMe19 小时前
【C/C++】namespace + macro混用场景
c语言·开发语言·c++
SY师弟21 小时前
台湾TEMI协会竞赛——0、竞赛介绍及开发板介绍
c语言·单片机·嵌入式硬件·嵌入式·台湾temi协会