C语言内存函数的模拟和使用

C语言内存函数的模拟和使用

    • [1. memcpy 使用和模拟实现](#1. memcpy 使用和模拟实现)
    • [2. memmove 使用和模拟实现](#2. memmove 使用和模拟实现)
    • [3. memset 函数的使用](#3. memset 函数的使用)
    • [4. memcmp 函数的使用](#4. memcmp 函数的使用)

1. memcpy 使用和模拟实现

c 复制代码
void * memcpy ( void * destination, const void * source, size_t num );
  • 函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。
  • 这个函数在遇到 '\0' 的时候并不会停下来。
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。

memcpy拷贝整形数组

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

运行结果如图:

memcpy函数拷贝字符型数组

c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char arr1[] = "hello";
	char arr2[10] = { 0 };
	memcpy(arr2, arr1, 5);
	int i = 0;
	for (i = 0; i < 5; i++)
	{
		printf("%c ", arr2[i]);
	}
	return 0;
}


memcpy函数的模拟实现:

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

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

c 复制代码
#include <stdio.h>
#include <assert.h>

void* my_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);
}

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

运行结果如图:

画图分析:

2. 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[10] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	memmove(arr1 + 2, arr1, 5 * sizeof(int));
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

运行结果如图:

memmove的模拟实现:

这里要分情况讨论,因为要把同一块空间前面的内容拷贝到后面,还是把后面的内容拷贝到前面是不同的情况,那应该怎么做呢?其实,很简单这里只需要记住要把前面的内容拷贝到后面就从后往前拷贝,即dest > src的时候,要把后面的内容拷贝到前面就从前往后拷贝,即dest < src的时候。

c 复制代码
#include <stdio.h>
#include <assert.h>

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

int main()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr, arr + 2, 5 * sizeof(int));
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr[i]);
	}
}

运行结果如图:

3. memset 函数的使用

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

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

c 复制代码
#include <stdio.h>
#include <string.h>

int main()
{
	char arr[20] = "hello world";
	memset(arr, 'x', 5);
	printf("%s\n", arr);
	
	return 0;
}

运行结果如图:

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

运行结果如图:

4. 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()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[] = { 1,2,3,4,8 };
	int ret = memcmp(arr1, arr2, 4 * sizeof(int));
	printf("%d\n", ret);
	return 0;
}

运行结果如图:

c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[] = { 1,2,3,4,8 };
	int ret = memcmp(arr1, arr2, 17);
	printf("%d\n", ret);
}

运行结果如图:

通过调试窗口可以观察到:

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

运行结果如图:

相关推荐
陕西企来客10 分钟前
生成式引擎优化(GEO)行业白皮书:从流量分配到认知占有的战略跃迁
人工智能·算法·机器学习·技术好geo优化
清泓y22 分钟前
深度学习算法
算法·ai·语言模型·面试
m沐沐41 分钟前
【深度学习】循环神经网络RNN——结构、原理与长期依赖问题解析
人工智能·pytorch·python·rnn·深度学习·算法·机器学习
Rabitebla1 小时前
C++ 内存管理全面复习:从内存分布到 operator new/delete
java·c语言·开发语言·c++·算法·leetcode
玖玥拾1 小时前
LeetCode 58 最后一个单词的长度
算法·leetcode
SilentSlot1 小时前
【C/C++】手写 DPDK 协议栈(八):用五元组 Hash 加速连接定位
c语言·c++·哈希算法
LONGZETECH1 小时前
工业实训仿真设计实践:电机拆装软件的 DAG 流程建模、工具精度分级与数据体系搭建
大数据·算法·unity·架构·汽车
m0_617493942 小时前
Python OpenCV 分水岭算法(Watershed)详解与实战
python·opencv·算法
海带紫菜菠萝汤2 小时前
企业批量PDF翻译方案:从选型到部署的完整指南
人工智能·算法·pdf
贵慜_Derek2 小时前
vLLM-05|MLA、Compressor、Indexer 与 SWA:Flash 注意力栈在 vLLM 里怎么落地
人工智能·算法·llm