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;
}
相关推荐
No0d1es2 小时前
电子学会青少年软件编程(C/C++)5级等级考试真题试卷(2024年6月)
c语言·c++·算法·青少年编程·电子学会·五级
Peter_Deng.6 小时前
Linux 下基于 TCP 的 C 语言客户端/服务器通信详解(三个示例逐步进阶)
服务器·c语言·网络
John.Lewis9 小时前
数据结构初阶(13)排序算法-选择排序(选择排序、堆排序)(动图演示)
c语言·数据结构·排序算法
丑小鸭是白天鹅12 小时前
嵌入式C语言学习笔记之枚举、联合体
c语言·笔记·学习
GUET_一路向前12 小时前
【C语言防御性编程】if条件常量在前,变量在后
c语言·开发语言·if-else·防御性编程
pusue_the_sun13 小时前
数据结构——栈和队列oj练习
c语言·数据结构·算法··队列
Dontla14 小时前
Makefile介绍(Makefile教程)(C/C++编译构建、自动化构建工具)
c语言·c++·自动化
奶黄小甜包14 小时前
C语言零基础第18讲:自定义类型—结构体
c语言·数据结构·笔记·学习
一支闲人14 小时前
C语言相关简单数据结构:双向链表
c语言·数据结构·链表·基础知识·适用于新手小白
John.Lewis15 小时前
数据结构初阶(19)外排序·文件归并排序的实现
c语言·数据结构·排序算法