C——语言内存函数

目录

一、memcpy的使用和模拟实现

1.memcpy函数原型

2.memcpy函数的使用

3.memcpy函数的模拟实现

二、memmove的使用和模拟实现

1.memmove函数原型

2.memmove函数的使用

3.memmove函数的模拟实现

三、memset的使用

1.memset函数原型

2.memset函数的使用

四、memcmp的使用

1.memcmp函数原型

2.memcmp函数的使用


一、memcpy的使用和模拟实现

1.memcpy函数原型
cpp 复制代码
 void * memcpy ( void * destination, const void * source, size_t num );

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

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

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

2.memcpy函数的使用
cpp 复制代码
#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);\\20个字节就是5个整形
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

看结果:

可以看到就是拷贝了5个整形。

上面为什么说遇到'\0'不会停下来呢?

接下来我给大家解释一下

看代码

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

这里我们的arr1字符数组里有一个'\0',我们现在要拷贝6个字节的内容到arr2中

我们看结果如何

我们可以看到,memcpy把'\0'也拷贝进去了。

3.memcpy函数的模拟实现
cpp 复制代码
#include <stdio.h>
#include <string.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()
{
	char arr1[] = { "abcesdaxc"};
	char arr2[10] = { 0 };
	my_memcpy(arr2, arr1, 3);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%c ", arr2[i]);
	}
	return 0;
}

为什么说如果source和destination有任何的重叠,复制的结果都是未定义的呢?

我们这里改一下模拟实现的代码,把自己复制给自己呢?

我们看一下

我们本来是想把abc复制到ces的位置上,但是我们发现拷贝过去是不正确的,所以结果是未定义的。

但是再VS上使用库函数memcpy是没有问题的,但是我们依旧再复制有重叠的时候不推荐使用memcpy,我们使用memmove。

二、memmove的使用和模拟实现

1.memmove函数原型
cpp 复制代码
void * memmove ( void * destination, const void * source, size_t num );

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

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

2.memmove函数的使用
cpp 复制代码
#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;
}

看结果:

3.memmove函数的模拟实现
cpp 复制代码
#include <stdio.h>
#include <string.h>
void* my_memmove(void* dst, const void* src, size_t count)
{
	void* ret = dst;
	if (dst <= src || (char*)dst >= ((char*)src + count)) {
		while (count--) 
		{
			*(char*)dst = *(char*)src;
			dst = (char*)dst + 1;
			src = (char*)src + 1;
		}
	}
	else {
		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);
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr1 , arr1+2, 20);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

三、memset的使用

1.memset函数原型
cpp 复制代码
void * memset ( void * ptr, int value, size_t num );

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

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

看结果:

四、memcmp的使用

1.memcmp函数原型
cpp 复制代码
int memcmp ( const void * ptr1, const void * ptr2, size_t num );

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

• 返回值如下:

2.memcmp函数的使用
cpp 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char buffer1[] = "DWgaOtP12df0";
	char buffer2[] = "DWGAOTP12DF0";
	int 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;
}

看结果:

注意:

对于memcmp(),如果两个字符串相同而且num大于字符串长度的话,memcmp不会在\0处停下来,会继续比较\0后面的内存单元,直到_res不为零或者达到num次数。

相关推荐
Wenweno0o1 天前
0基础Go语言Eino框架智能体实战-chatModel
开发语言·后端·golang
chenjingming6661 天前
jmeter线程组设置以及串行和并行设置
java·开发语言·jmeter
qq_339554821 天前
英飞凌ModusToolbox环境搭建
c语言·eclipse
cch89181 天前
Python主流框架全解析
开发语言·python
不爱吃炸鸡柳1 天前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发1 天前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
Momentary_SixthSense1 天前
设计模式之工厂模式
java·开发语言·设计模式
‎ദ്ദിᵔ.˛.ᵔ₎1 天前
STL 栈 队列
开发语言·c++
勿忘,瞬间1 天前
数据结构—顺序表
java·开发语言
张張4081 天前
(域格)环境搭建和编译
c语言·开发语言·python·ai