内存操作函数

memcpy

mem--memory--内存 指向计算机内存 cpy-copy-拷贝 也就是内存拷贝 针对内存的函数

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

把source的空间 复制到 destination的部分 长度是num

如果source 和 dest 的部分有重叠,会复制的结果是未定义的(建议不要有重叠的内存)

而且会返回目标空间的起始地址

cpp 复制代码
int main()
{
	int arr1[] = { 1,2,3,4,5 };
	int arr2[10] = { 0 };
	memcpy(arr2, arr1, 20);
	for (int i = 0; i < 5; i++)
		printf("%d", arr2[i]);
}

模拟实现

cpp 复制代码
void* my_memcpy(void* d, const void* s, size_t num)
{
	assert(s && d);
	void* ret = d;
	int i = 0;
	while (num--)
	{
		*(char*)d = *(char*)s;
		s = (char*)s + 1;
		d = (char*)d + 1;
	}
	return ret;
}

memmove

它和memcpy几乎一样,但是可以操作重叠的内存

模拟实现

cpp 复制代码
void* my_memmove(void*dest,const void* src,size_t num)
{
	assert(dest && src);
	void* ret = dest;
	if (dest < src)
	{
		while (num--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		while (num--)
		{
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	return ret;
}

memset

这个函数是用于设置内存的

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

ptr 指向的内存 value是更改的类型 num是大小

memcmp

完成内存块的比较

cpp 复制代码
int memcmp( const void * ptr1,const void * ptr2,size_t num);
相关推荐
wuminyu2 小时前
JVM规范学习总结:解释器与JIT编译器如何处理JVM字节码
java·linux·c语言·jvm·c++
小徐不徐说2 小时前
序列化与反序列化:把 C++ 对象可靠地保存下来
开发语言·数据库·c++·php
ragnwang3 小时前
Notes: Python pybind11 import C++ module
开发语言·c++·python
Wang's Blog3 小时前
Java 项目实战: 交友项目 - 搜索附近的功能概述和分析
java·开发语言·交友
骑着蜗牛撵大象3273 小时前
大模型 Agent 的沙箱护栏:gVisor 隔离、出口白名单与审计闭环
java·开发语言·jvm·安全·agent沙箱·gvisor·microvm
Dxy12393102164 小时前
JS DES 加密详解
开发语言·javascript·arcgis
充值内卷4 小时前
我给桌面加了个鱼缸:C++ 写后端,WebView2 里养一座 Three.js 小岛
开发语言·javascript·c++
云和数据.ChenGuang4 小时前
深入解析 Python 的 dir():从用法到 CPython 源码实现
开发语言·人工智能·python·langchain
nice_lcj5205 小时前
深入理解 Java 反射:从 Class 对象到方法调用(含性能实测 + 迷你 IOC 实战 + 面试考点)
java·开发语言·后端·面试
樱花树下的数学喵7 小时前
手写简易 STL string|C++ 字符串底层实现
开发语言·c++