(c语言进阶)内存函数

一.memcpy(void* dest,void* src,int num) ,操作单位为字节,完成复制且粘贴字符串

1.应用

cpp 复制代码
#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);//从arr1中读取20个字节,将其复制给arr2
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

2.模拟实现

cpp 复制代码
#include <stdio.h>
#include<string.h>
#include<assert.h>
//因为该函数需要能操控任意类型的数据,故不能将参数设定为固定类型
//而void类型可以接收任意类型的值
void* my_memcpy(void* arr2,const void* arr1,int x)//将arr1中的元素拷贝给arr2
{
	assert(arr1&&arr2);	//不能为空指针
	void* ret = arr2;  //保存被赋值数组的首地址
	while (x--)
	{
		*(char*)arr2 = *(char*)arr1;	//void*类型无法解引用,需要转换为其他类型
		arr1 = (char*)arr1 + 1;			//转化为char*类型,可每次只改变一个字节,可适用于任何类型
		arr2 = (char*)arr2 + 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);//从arr1中读取20个字节,将其复制给arr2
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

3.重点------memcpy()可以处理,但不用来处理重叠内存之间的数据拷贝(这种问题统一交给memmove函数------分工明确)

cpp 复制代码
#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(arr1+2, arr1, 20);//从arr1中读取20个字节,将其复制给arr2
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

二.memmove(void* dest,void* src,int num) ------解决重叠内存之间的数据拷贝

1.应用

cpp 复制代码
#include <stdio.h>
#include<string.h>
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	memmove(arr1 + 2, arr1, 20);//从arr1中读取20个字节,将其复制给arr2
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

2.模拟实现

cpp 复制代码
#include <stdio.h>
#include<string.h>
#include<assert.h>
void* my_memmove(void* arr2 ,void* arr1, int x)//将arr1中的元素拷贝给arr2
{
	void* p = arr2;
	assert(arr2&&arr1);
	if (arr2 <= arr1)
	{
		while (x--)
		{
			*(char*)arr2 =*(char*)arr1;
			arr1=(char*)arr1+1;
			arr2=(char*)arr2+1;
		}
	}
	else
	{
		while (x--)
		{
			*((char*)arr2 + x) = *((char*)arr1+x);	//将指针指向需要赋值的空间最后-1,每次向前读取一个字节
		}
	}
	return p;  //返回被拷贝的数组首地址
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	my_memmove(arr1 + 2, arr1, 28);//从arr1中读取20个字节,将其复制给arr1+2
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

三.memcmp()------内存比较函数,比较单位为字节,可以比较任意类型的元素

1.应用

cpp 复制代码
#include<stdio.h>
#include<string.h>
int main()
{
	int arr1[] = { 1,2,3,4,5};  //01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00
	int arr2[20] = { 1,3,2 };	//01 00 00 00 03 00 00 00 02 00 00 00
	int ret = memcmp(arr1,arr2,12);//比较两个数组中前12个字节
	//arr1>arr2返回值为正整数,arr1=arr2返回值为0,arr1<arr2返回值为负整数
	printf("%d",ret);
	return 0;
}

2.模拟实现

cpp 复制代码
#include<stdio.h>
#include<string.h>
#include<assert.h>
int my_memcmp(void* arr1, void* arr2, int x)
{
	assert(arr1 && arr2);
	while (*(char*)arr1 == *(char*)arr2) //相同的情况下,判断下一位是否也相同,不相同则退出循环
	{
		if (x==0)  //若有其中一个字符串到达末尾,则退出循环
		{
			break;
		}
		arr1=(char*)arr1+1;
		arr2=(char*)arr2+1;
		x--;
	}
	return *(char*)arr1 - *(char*)arr2;  //不相同则相减返回差值
}
int main()
{
	int arr1[] = { 1,2,3,4,5};  //01 00 00 00 02 00 00 00 03 00 00 00 04 00 00 00 05 00 00 00
	int arr2[20] = { 1,3,3 };	//01 00 00 00 03 00 00 00 02 00 00 00
	int ret = my_memcmp(arr1,arr2,12);//比较两个数组中前12个字节
	//arr1>arr2返回值为正整数,arr1=arr2返回值为0,arr1<arr2返回值为负整数
	printf("%d",ret);
	return 0;
}

四.memset()------内存初始化(初始化变量为整形或字符型)

1.应用

cpp 复制代码
#include<stdio.h>
#include<string.h>
int main()
{
	char arr[] = "hello bit";
	memset(arr,'x',5);  //将arr地址后五个字节的内存初始化为'x'
	printf("%s",arr);
	return 0;
}

2.模拟实现

cpp 复制代码
#include<stdio.h>
#include<string.h>
void* my_memset(void* arr, int value, int x)
{
	void* p = arr;
	while (x--)
	{
		*(char*)arr = value;
		arr=(char*)arr + 1;
	}
	return p;
}
int main()
{
	char arr[] = "hello bit";
	my_memset(arr+6,'1',3);
	printf("%s",arr);
	return 0;
}
相关推荐
2013092416273 小时前
1968年 Hart, Nilsson, Raphael 《最小成本路径启发式确定的形式基础》A* 算法深度研究报告
人工智能·算法
如何原谅奋力过但无声3 小时前
【力扣-Python-滑动窗口经典题】567.字符串的排列 | 424.替换后的最长重复字符 | 76.最小覆盖子串
算法·leetcode
玄冥剑尊4 小时前
贪心算法进阶
算法·贪心算法
生擒小朵拉4 小时前
ROS1学习笔记(二)
笔记·学习
玄冥剑尊4 小时前
贪心算法深化 I
算法·贪心算法
52Hz1184 小时前
力扣73.矩阵置零、54.螺旋矩阵、48.旋转图像
python·算法·leetcode·矩阵
BHXDML4 小时前
第一章:线性回归& 逻辑回归
算法·逻辑回归·线性回归
iAkuya5 小时前
(leetcode)力扣100 二叉搜索树种第K小的元素(中序遍历||记录子树的节点数)
算法·leetcode·职场和发展
Root_Hacker5 小时前
include文件包含个人笔记及c底层调试
android·linux·服务器·c语言·笔记·安全·php
杨间5 小时前
《排序算法全解析:从基础到优化,一文吃透八大排序!》
c语言·数据结构·排序算法