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

运行结果如图:

相关推荐
2601_949146532 小时前
C语言语音通知API示例代码:基于标准C的语音接口开发与底层调用实践
c语言·开发语言
学嵌入式的小杨同学2 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
wfeqhfxz25887822 小时前
YOLO13-C3k2-GhostDynamicConv烟雾检测算法实现与优化
人工智能·算法·计算机视觉
Aaron15883 小时前
基于RFSOC的数字射频存储技术应用分析
c语言·人工智能·驱动开发·算法·fpga开发·硬件工程·信号处理
Queenie_Charlie3 小时前
前缀和的前缀和
数据结构·c++·树状数组
爱编码的小八嘎4 小时前
C语言对话-21.模板特化,缺省参数和其他一些有趣的事情
c语言
_不会dp不改名_4 小时前
leetcode_3010 将数组分成最小总代价的子数组 I
算法·leetcode·职场和发展
yueyuexiaokeai15 小时前
linux kernel常用函数整理
linux·c语言
你撅嘴真丑6 小时前
字符环 与 变换的矩阵
算法
想放学的刺客6 小时前
单片机嵌入式试题(第29期)嵌入式系统的电源完整性设计与去耦电容选型。抗干扰设计与EMC合规性
c语言·stm32·嵌入式硬件·物联网·51单片机