C语⾔内存函数

内存函数

1.memcpy使用和模拟实现

memcpy的官方详解

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

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	memcpy(arr2, arr1, 20);//20字节 = 5 个整型

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);//1 2 3 4 5 0 0 0 0 0
	}

	return 0;
}

对于重叠的内存,交给memmove来处理。

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

void* my_memcpy(void* dest, const void* src, size_t num)
{
	void* ret = dest;
	assert(dest && src);

	while (num--)
	{
		*(char*)dest = *(char*)src;
		dest = (char*)dest + 1;
		src = (char*)src + 1;
	}
	return ret;
}

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[20] = { 0 };
	my_memcpy(arr2, arr1, 20);//20字节 = 5 个整型

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);//1 2 3 4 5 0 0 0 0 0
	}

	return 0;
}

2.memmove使用和模拟实现

memmove的官方详解

c 复制代码
void* memmove(void* destination, const void* source, size_t num);
  1. 与memcpy的差别就是memmove函数处理的源内存块和目标内存块是可以重叠的。
  2. 如果源空间和目标空间出现重叠,就得使用memmove函数处理。
c 复制代码
#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);

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}

	return 0;
}

输出的结果:

c 复制代码
1 2 1 2 3 4 5 8 9 10

memmove的模拟实现:

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

void* my_memmove(void* dest, const void* src, size_t num)
{
	void* ret = dest;
	assert(dest && src);
	if (dest <= src || (char*)dest >= (char*)src + num)
	{
		//前 -> 后
		while (num--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		//后 -> 前
		dest = (char*)dest + num - 1;
		src = (char*)src + num - 1;

		while (num--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest - 1;
			src = (char*)src - 1;
		}
	}

	return ret;
}

int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	my_memmove(arr1 + 2, arr1, 20);

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}

	return 0;
}

3.memset的使用

memset的官方详解

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

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

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

int main()
{
	char str[] = "hello world";
	memset(str, 'x', 5);
	printf(str);

	return 0;
}

输出的结果:

c 复制代码
xxxxx world

4.memcmp函数的使用

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()
{
	char str1[] = "abcdef11";
	char str2[] = "acvdef22";
	int n = memcmp(str1, str2, sizeof(str1));

	if (n > 0)
		printf("'%s' is greater than '%s'.\n", str1, str2);
	else if(n < 0)
		printf("'%s' is less than '%s'.\n", str1, str2);
	else
		printf("'%s' is the same as '%s'.\n", str1, str2);

	return 0;
}
相关推荐
逐步前行24 分钟前
C标准库--C99--布尔型<stdbool.h>
c语言·开发语言
SunnyKriSmile24 分钟前
C语言译码操作
c语言·算法·if语句·译码操作·switch语句
傻童:CPU25 分钟前
C语言需要掌握的基础知识点之线性表
c语言·1024程序员节
Heavy sea1 小时前
Linux串口应用编程
linux·c语言·1024程序员节
再睡一夏就好1 小时前
【C++闯关笔记】详解多态
c语言·c++·笔记·学习·语法·1024程序员节
奔跑吧邓邓子5 小时前
【C语言实战(44)】C语言打造全能简易计算器:突破运算极限
c语言·实战·全能简易计算器
小志biubiu8 小时前
linux_缓冲区及简单libc库【Ubuntu】
linux·运维·服务器·c语言·学习·ubuntu·c
MeowKnight9588 小时前
【C】函数指针
c语言·1024程序员节
胜天半月子9 小时前
嵌入式开发 | C语言 | 单精度浮点数4字节可以表示的范围计算过程
c语言·嵌入式c·1024程序员节·单精度浮点数计算
m0_7482336410 小时前
单调栈详解【C/C++】
c语言·c++·算法·1024程序员节