关于C语⾔内存函数 memcpy memmove memset memcmp

memcpy使⽤和模拟实现

void * memcpy ( void * destination, const void * source, size_t num );

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

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

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

使用

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

拷贝结束后返回目标空间的起始地址

模拟实现

cs 复制代码
#include <string.h>
#include <stdio.h>
#include<assert.h>
void* my_memcpy(void* dst, const void* src, size_t count)
{
	void* ret = dst;
	assert(dst);
	assert(src);
	while (count--) {
		*(char*)dst = *(char*)src;
		dst = (char*)dst + 1;
		src = (char*)src + 1;
	}
	return(ret);
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memcpy(arr2, arr1, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

memmove 使**⽤**和模拟实现

void * memmove ( void * destination, const void * source, size_t num );

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

如果源空间和⽬标空间出现重叠,就得使⽤memmove函数处理。

使用

cs 复制代码
#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 ", arr1[i]);
 }
return 0;
}

模拟实现

分情况1

情况2

有的情况从后向前拷贝,有的从前向后拷贝,分析总结

模拟实现代码

cs 复制代码
//模拟实现memmove
//void * memmove ( void * destination, const void * source, size_t num );
#include<stdio.h>
void* my_memmove(void* dest, const void* source, size_t num)
{
	void* ret = dest;
	if (dest < source)//dest 在src左边,从前向后拷贝
	{
		while (num--)
		{
			*(char*)dest = *(char*)source;
			dest=(char*)dest + 1;
			source=(char*)source + 1;
		}
	}
	else {//dest 在src右边以及重合,从后向前拷贝
		while (num--)
		{
			*((char*)dest + num) = *((char*)source + num);
		}
	}
	return ret;
}
int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
	my_memmove(arr + 2, arr, 20);
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
	return 0;
}

结果

memset 函数的使⽤

memset是⽤来设置内存的,将内存中的值 以字节为单位 设置成想要的内容。
void * memset ( void * ptr, int value, size_t num );

使用

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

输出的结果:
xxxxxxworld

memcmp 函数的使⽤

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

与str系类相似但是sy=tr系类只能比较字符串

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

使用,此函数比较内存块,任何类型都可以不受限制

与str系类相似但是sy=tr系类只能比较字符串

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;
}
相关推荐
枯萎穿心攻击2 小时前
响应式编程入门教程第二节:构建 ObservableProperty<T> — 封装 ReactiveProperty 的高级用法
开发语言·unity·c#·游戏引擎
DKPT2 小时前
Java桥接模式实现方式与测试方法
java·笔记·学习·设计模式·桥接模式
Eiceblue3 小时前
【免费.NET方案】CSV到PDF与DataTable的快速转换
开发语言·pdf·c#·.net
巴伦是只猫4 小时前
【机器学习笔记Ⅰ】13 正则化代价函数
人工智能·笔记·机器学习
学不动CV了4 小时前
ARM单片机启动流程(二)(详细解析)
c语言·arm开发·stm32·单片机·51单片机
猫猫的小茶馆6 小时前
【STM32】通用定时器基本原理
c语言·stm32·单片机·嵌入式硬件·mcu·51单片机
pumpkin845147 小时前
Rust 调用 C 函数的 FFI
c语言·算法·rust
2401_858286118 小时前
125.【C语言】数据结构之归并排序递归解法
c语言·开发语言·数据结构·算法·排序算法·归并排序
双叶8369 小时前
(C++)学生管理系统(正式版)(map数组的应用)(string应用)(引用)(文件储存的应用)(C++教学)(C++项目)
c语言·开发语言·数据结构·c++
X_StarX10 小时前
【Unity笔记02】订阅事件-自动开门
笔记·学习·unity·游戏引擎·游戏开发·大学生