【C语言】内存函数

memcpy使用和模拟实现

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

介绍:

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

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

• 如果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[10] = { 0 };
	 memcpy(arr2, arr1, 20);
	 int i = 0;
	 for (i = 0; i < 10; i++)
	 {
		 printf("%d ", arr2[i]);
	 }
	 return 0;
}

对于重叠的内存,交给memmove来处理

模拟实现:

c 复制代码
void * memcpy ( void * dst, const void * src, size_t count)
{
	 void * ret = dst;
	 assert(dst);
	 assert(src);
	 /*
	 * copy from lower addresses to higher addresses
	 */
	 while (count--) 
	 {
		 *(char *)dst = *(char *)src;
		 dst = (char *)dst + 1;
		 src = (char *)src + 1;
	 }
	 return(ret);
}

memmove 使用和模拟实现

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

介绍:

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

• 如果源空间和目标空间出现重叠,就得使用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);
	 int i = 0;
	 for (i = 0; i < 10; i++)
	 {
	 	printf("%d ", arr2[i]);
	 }
	 return 0;
}

运行结果:

1 2 1 2 3 4 5 8 9 10

模拟实现

c 复制代码
void * memmove ( void * dst, const void * src, size_t count)
{
	 void * ret = dst;
	 if (dst <= src || (char *)dst >= ((char *)src + count)) {
	 /*
	 * Non-Overlapping Buffers
	 * copy from lower addresses to higher addresses
	 */
		 while (count--) 
		 {
			 *(char *)dst = *(char *)src;
			 dst = (char *)dst + 1;
			 src = (char *)src + 1;
		 }
	 }
	 else 
	 {
	 /*
	 * Overlapping Buffers
	 * copy from higher addresses to lower addresses
	 */
		 dst = (char *)dst + count - 1;
		 src = (char *)src + count - 1;
		 while (count--) 
		 {
			 *(char *)dst = *(char *)src;
			 dst = (char *)dst - 1;
			 src = (char *)src - 1;
		 }
	 }
 	 return(ret);
}

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',6);
 printf(str);
 return 0;
}

运行结果:

xxxxxxworld

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 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;
}
相关推荐
小小测试开发3 小时前
安装 Python 3.10+
开发语言·人工智能·python
我叫唧唧波4 小时前
Python+AI 全栈学习笔记
人工智能·python·学习
AAA大运重卡何师傅(专跑国道)4 小时前
【无标题】
开发语言·c#
是阿建吖!4 小时前
【Linux】信号
android·linux·c语言·c++
城北徐宫4 小时前
Linux信号深度解剖:5种产生、3张表、4次切换
linux·c++·学习
XBodhi.4 小时前
Visual Studio C++ 语法错误: 缺少“;”(在“return”的前面)
开发语言·c++·visual studio
LSssT.5 小时前
【01】Python 机器学习
开发语言·python
三品吉他手会点灯5 小时前
C语言学习笔记 - 43.运算符与表达式 - 运算符1 - 运算符的分类和简单介绍
c语言·笔记·学习·算法
l1t6 小时前
DeepSeek总结的使用实体-组件-系统和基于存在性处理进行Python编程39-40
开发语言·python
曾阿伦6 小时前
Python 搭建简易HTTP服务
开发语言·python·http