C语言——内存函数的使用与模拟实现

大家好,我是残念,希望在你看完之后,能对你有所帮助,有什么不足请指正!共同学习交流
本文由:残念ing 原创CSDN首发,如需要转载请通知
个人主页:残念ing-CSDN博客,欢迎各位→点赞👍 + 收藏⭐️ + 留言📝
📣系列专栏:残念ing 的C语言系列专栏------CSDN博客


目录

前言:

[1. memcpy 函数](#1. memcpy 函数)

[1.1 memcpy 的使用](#1.1 memcpy 的使用)

[1.2 memcpy 的模拟实现](#1.2 memcpy 的模拟实现)

[2. memmove 函数](#2. memmove 函数)

[2.1 memmove 的使用](#2.1 memmove 的使用)

[2.2 memmove 的模拟实现](#2.2 memmove 的模拟实现)

[3. memset 函数的使用](#3. memset 函数的使用)

[4. memcmp 函数的使用](#4. memcmp 函数的使用)


前言:

在C语言中除了字符函数和字符串函数外,还有关于内存的函数,现在我们就来学习一下内存函数吧!!!

1. memcpy 函数

cs 复制代码
void * memcpy ( void * destination, const void * source, size_t num );

功能:函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置

注意:

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

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

1.1 memcpy 的使用

cs 复制代码
#include<stdio.h>
#include<string.h>
//memcpy的使用--拷贝(有开始地址,拷贝的数)
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr2[20] = { 0 };
	memcpy(arr2, arr1+3, 5 * sizeof(int));
	for (int i = 0; i < 20; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

1.2 memcpy 的模拟实现

cs 复制代码
//模拟实现
void* my_memcpy(void* dest, const void* src, size_t num)
{
	while (num--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return dest;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr2[20] = { 0 };
	void*ret=my_memcpy(arr2, arr1 + 3, 5 * sizeof(int));
	for (int i = 0; i < 20; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

2. memmove 函数

cs 复制代码
void * memmove ( void * destination, const void * source, size_t num );

功能:从source的位置开始向后复制num个字节的数据到destination指向的内存位置

注意:

1、和memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的

2、如果源内存空间和目标空间出现重叠,就得使用memmove函数处理

2.1 memmove 的使用

cs 复制代码
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,0 };
	int arr2[20] = { 0 };
	memmove(arr1+2, arr1, 5 * sizeof(int));
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

2.2 memmove 的模拟实现

cs 复制代码
void* memmove(void* dst, const void* src, size_t count)
{
	void* ret = dst;//记住起始位置
	if (dst <= src || (char*)dst >= ((char*)src + count)) 
    {
        //从前往后拷
		while (count--) 
        {
			*(char*)dst = *(char*)src;
			dst = (char*)dst + 1;
			src = (char*)src + 1;
		}
	}
	else 
    {
        //从后往前拷
		while (count--) 
        {
			*((char*)dst+count) = *((char*)src+count);
		}
	}
	return ret;
}

3. memset 函数的使用

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

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

cs 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "hello world";
	memset(str, 'x', 6);
	printf(str);
	return 0;
}

4. memcmp 函数的使用

cs 复制代码
int memcmp ( const void * ptr1, const void * ptr2, size_t num );

功能:比较从ptr1和ptr2指针指向的位置开始,向后的num个字节

返回规则:

cs 复制代码
#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;
}
相关推荐
shinelord明6 分钟前
【Python】Python知识总结浅析
开发语言·人工智能·python
浅陌pa10 分钟前
01:(寄存器开发)点亮一个LED灯
c语言·stm32·单片机·嵌入式硬件
吹老师个人app编程教学11 分钟前
阿里巴巴_java开发规范手册详解
java·开发语言
初阳78530 分钟前
【Qt】控件概述(4)—— 输出类控件
开发语言·qt·命令模式
大白_dev33 分钟前
数据校验的总结
java·开发语言
武昌库里写JAVA34 分钟前
Vue3常用API总结
数据结构·spring boot·算法·bootstrap·课程设计
C++忠实粉丝36 分钟前
位运算(7)_消失的两个数字
算法
卑微求AC36 分钟前
(C语言贪吃蛇)4.贪吃蛇地图优化及算法说明
c语言·算法
sjsjs1136 分钟前
【动态规划-最长公共子序列(LCS)】【hard】力扣1458. 两个子序列的最大点积
算法·leetcode·动态规划
雷神乐乐37 分钟前
Python常用函数
开发语言·python