C语言中内存函数的使用

memcpy函数的使用和模拟实现

memcpy的使用

函数使用说明:
• 函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。
• 这个函数在遇到 '\0' 的时候并不会停下来。
• 如果source和destination有任何的重叠,复制的结果都是未定义的。

cpp 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
 int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
 int arr2[10] = { 0 };
 memcpy(arr2, arr1, 20);
 int i = 0;
 for (i = 0; i < 10; i++)
 {
 printf("%d ", arr2[i]);
 }
 return 0;
}

memcpy的模拟实现

cpp 复制代码
#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, 40);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

memmove函数的使用和模拟实现

memmove的使用

功能与memcpy类似,对于重叠的内存,交给memmove来处理。

cpp 复制代码
#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;
}

memmove的模拟实现

cpp 复制代码
#include<stdio.h>
#include<assert.h>
void* memmove(void* dest,const void* src, size_t num)
{
	assert(dest && src);
	void* ret = dest;
	if (dest < src)
	{
		while (num--)
		{
			*(char*)dest = *(char*)src;
			(*(char*)dest)++;
			(*(char*)src)++;
		}
	}
	else
	{
		while (num--)
		{
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	return ret;

}
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;
}

对于memmove函数的模拟,最关键的地方在于对*src 和 *dest位置的判断:

如果 dest 的地址小于 src 的地址,那内存的修改就需要从前向后;如果dest 的地址大于 src 的地址,那内存的修改就需要从后相前;如果两块空间无重叠,那么两种方式都可以。

memset的使用


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

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

对于memset的使用多说一嘴,如果使将整型值通过函数memset来修改,是按照以字节为单位来修改的,即 memset (str,1,4);的作用使将第一个整型的十六进制修改为01010101.结果使一个相当大的数,而不是将前四个元素修改为1.

memcmp函数的使用

• ⽐较从ptr1和ptr2指针指向的位置开始,向后的num个字节
返回值:

cpp 复制代码
#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;
}

memcmp和strcmp的比较方式差不多,区别在于memcmp是在内存的视角比较,strcmp是在ascii值的层面比较。

相关推荐
HABuo3 小时前
【linux文件系统】磁盘结构&文件系统详谈
linux·运维·服务器·c语言·c++·ubuntu·centos
2401_858936888 小时前
【Linux C 编程】标准 IO 详解与实战:从基础接口到文件操作实战
linux·c语言
季明洵10 小时前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
浅念-11 小时前
C语言编译与链接全流程:从源码到可执行程序的幕后之旅
c语言·开发语言·数据结构·经验分享·笔记·学习·算法
爱吃生蚝的于勒11 小时前
【Linux】进程信号之捕捉(三)
linux·运维·服务器·c语言·数据结构·c++·学习
The森11 小时前
Linux IO 模型纵深解析 01:从 Unix 传统到 Linux 内核的 IO 第一性原理
linux·服务器·c语言·经验分享·笔记·unix
C++ 老炮儿的技术栈12 小时前
Qt 编写 TcpClient 程序 详细步骤
c语言·开发语言·数据库·c++·qt·算法
wangjialelele13 小时前
Linux下的IO操作以及ext系列文件系统
linux·运维·服务器·c语言·c++·个人开发
wengqidaifeng15 小时前
数据结构(三)栈和队列(上)栈:计算机世界的“叠叠乐”
c语言·数据结构·数据库·链表
VekiSon15 小时前
Linux内核驱动——设备树原理与应用
linux·c语言·arm开发·嵌入式硬件