C语言内存函数



🦆 个人主页:深邃-

❄️专栏传送门:《C语言》《数据结构》

🌟Gitee仓库:《C语言》《数据结构》


上个文章,strcpy strncpy 拷贝字符串的,是有局限性的 拷贝一个整型数组呢?结构体数组呢?
接下来就能学到如何拷贝除了字符串以外的类型

目录

memcpy使用和模拟实现

memcpy的概念

c 复制代码
void * memcpy ( void * destination, const void * source, size_t num );
  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。

  • 这个函数在遇到 '\0' 的时候并不会停下来。

  • 如果source和destination有任何的重叠,复制的结果都是未定义的。

  • 头文件#include<string.h>

memcpy的使用

c 复制代码
#include<stdio.h>
#include<string.h>
int main()
{
	char arr1[10] = { '1','2','3','\0','4','5'};
	char arr2[10] = { '0'};
	memcpy(arr2, arr1, 6);
	return 0;
}


因此可见\0也可以拷贝过来,拷贝的多少只跟num(单位字节)大小有关

c 复制代码
#include<stdio.h>
#include<string.h>
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	//             0 1 2 3 4 5 ...
	int arr2[20] = { 0 };
	//             0 1 2 3 4 5 ...
	//想把arr1中的10个整数,拷贝到arr2中
	//循环遍历
	//memcpy
	memcpy(arr2, arr1, 40);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

memcpy的模拟实现

思路:一个一个字节拷贝过来

c 复制代码
void* my_memcpy(void* dest, const void* src, size_t num)
{
	void* ret = dest;
	assert(dest && src);
	while (num--)
	{
		*(char*)dest = *(char*)src;
		src = (char*)src + 1;
		dest = (char*)dest + 1;
	}
	return ret;
}

疑问:为什么已经强制类型转换了,还要再次强转

  • *(char * )dest 里的 (char * ),是一次性的、临时的强制类型转换:它只作用于当前这一次解引用操作,不会修改 dest 这个变量本身的类型

疑问:为什么不能 * (char * )dest ++= * (char * )src++; 这样写:

  • ++ 优先级高于强转,先对void*执行++,就会报错

memmove使用和模拟实现

memmove的概念

memcpy不重叠就行
memmove重叠的和不重叠的都可以(有些编译器实现两者库函数相同,如vs2022)

c 复制代码
void * memmove ( void * destination, const void * source, size_t num );
  • 和memcpy的差别就是memmove函数处理的源内存快和目标内存块是可以重叠的。
  • 如果源空间和目标空间出现重叠,就得使用memmove函数处理。

memmove的使用

内存重叠 内存重叠 内存重叠

c 复制代码
#include <stdio.h>
#include <string.h>
#include <assert.h>
int main()
{
	//内存重叠
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	memmove(arr+2, arr, 20);

	return 0;
}

memmove的模拟实现

思路:如果dest在src的前面,那么从前向后拷贝是不会重叠的
同理,如果src在dest的前面,那么从后向前拷贝是不会重叠的

c 复制代码
void* my_memmove(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;
	if (dest < src) //1
	{
		//前->后
		while (num--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else //2 3(包含重合的情况)
	{
		//后->前
		while (num--) //num = 3
		{             
			//num = 2
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	return ret;
}

memset函数的使用

memset的使用

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

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

c 复制代码
#include <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
	char arr[] = "hello world";
	memset(arr+2, 'x', 5);
	printf("%s\n", arr);
	return 0;
}

memset的使用错误

因为memset的是以字节为单位进行修改,一次只更改一个字节

能否将arr的每个元素设置为1

c 复制代码
#include <string.h>
#include <stdio.h>
#include <string.h>
//能否将arr的每个元素设置为1
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	memset(arr, 1, 40);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

打印结果 打印结果 打印结果

内存 内存 内存

能否将arr的每个元素设置为0

只有在都改成0的时候才没有问题

cpp 复制代码
#include <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
	int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
	memset(arr, 0, 40);
	return 0;
}

memcmp函数的使用

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

Return Value

Returns an integral value indicating the relationship between the content of the memory blocks:

return value indicates
<0 the first byte that does not match in both memory blocks has a lower value in ptr1 than in ptr2 (if evaluated as unsigned char values)
0 the contents of both memory blocks are equal
>0 the first byte that does not match in both memory blocks has a greater value in ptr1 than in ptr2 (if evaluated as unsigned char values)

使用 使用 使用

c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char buffer1[] = "DWgaOtP12df0";
	char buffer2[] = "DWGAOTP12DF0";
	int n;
	n = memcmp(buffer1, buffer2, sizeof(buffer1));
	if (n > 0)
		printf("'%s' is greater than '%s'.\n", buffer1, buffer2);
	else if (n < 0)
		printf("'%s' is less than '%s'.\n", buffer1, buffer2);
	else
		printf("'%s' is the same as '%s'.\n", buffer1, buffer2);
	return 0;
}
相关推荐
炽烈小老头2 小时前
【每天学习一点算法 2026/04/03】二叉树的序列化与反序列化
学习·算法
码界筑梦坊2 小时前
324-基于Python的中国传染病数据可视化分析系统
开发语言·python·信息可视化
源码之家2 小时前
计算机毕业设计:Python汽车销量智能分析与预测系统 Flask框架 scikit-learn 可视化 requests爬虫 AI 大模型(建议收藏)✅
人工智能·hadoop·python·算法·数据分析·flask·课程设计
旺仔.2912 小时前
八大排序:(三)快速排序
数据结构·c++·算法
北顾笙9802 小时前
day13-数据结构力扣
数据结构·算法·leetcode
生信研究猿2 小时前
leetcode 1.两数之和(重刷)
算法·leetcode·职场和发展
吕司2 小时前
Linux线程的操作
linux·服务器·开发语言
曹牧2 小时前
Java:上传文件到网页
java·开发语言
ZoeJoy82 小时前
算法筑基(八):数学算法——程序背后的数理根基
算法·贪心算法·排序算法·动态规划·图搜索算法