C语言学习系列-->一篇带你看懂内存函数

文章目录

前言

上篇文章学习了C语言字符串函数,只是对字符串进行操作

本节,小编整理了一下C语言中的内存函数,对内存进行操作,只针对会内存块,不针对数据

memcpy

概述

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

memcpy是对内存拷贝

拷贝的可能是字符串,也可能是整型数组

所以使用 void*

将source拷贝到destination,指定字节数为num

code

c 复制代码
#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);
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

arr2是dest,arr1是scr,20是怒num,即字节

将arr1中的前20个字节(即1,2,3,4,5)复制到arr2中

运行结果

c 复制代码
1 2 3 4 5 0 0 0 0 0

模拟实现

#include <stdio.h>

#include <string.h>

#include<assert.h>

c 复制代码
void* my_memcpy(void* dest,const void* src, size_t num)
{
	void* ret = dest;    //后面dest的地址可能会发生改变,因此先将dest的首地址存放在ret中
	assert(dest && src);
	while (num--)
	{
		*(char*) dest = *(char*)src;   //viod*型不能解引用,所以先强制类型转换成char*型,再解引用
		dest = (char*)dest+1;
		src = (char*)src+1;
	}
}

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

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

其实在vs中,memcpy库函数也可以处理重叠内存,但是在其他编译器不一定可以,不通用

memmove

概述

c 复制代码
void * memmove ( void * destination, const void * source, size_t num );

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

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

将1 2 3 4 5拷贝到3 4 5 6 7的位置上

即,最终结果为,1 2 1 2 3 4 5 8 9 10

code

c 复制代码
#include <stdio.h>
#include <string.h>
#include<assert.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;
}

输出结果

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

模拟实现

前面我们看到,

将1 2 3 4 5拷贝到3 4 5 6 7的位置上

实际上应该是

将5放到7的位置

将4放到6的位置

将3放到5的位置

将4放到4的位置

将3放到3的位置

是后--->前

如果,先将1放到3的位置,那么3这个位置就变成1。再将2放到4的位置,此时最终结果就变成了1 2 1 2 1 2 1 8 9 10

再来看看这种情况:

将5 6 7 8 9拷贝到3 4 5 6 7的位置上

将5放到3的位置上

将6放到4的位置上

(此时原本5 6位置上就没有数字了)

将7放到5的位置

将8放到6的位置

将9放到7的位置

是前--->后

因此,

if(dest<src):前--->后

else:后--->前

模拟code

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);
	assert(src);

	//分情况讨论
	if (dest < src)
	{
		//前->后
		while (num--)
		{
			*(char*)dest = *(char*)src;
			dest = (char*)dest + 1;
			src = (char*)src + 1;
		}
	}
	else
	{
		//后->前
		while (num--)
		{
			//num=18
			*((char*)dest + num) = *((char*)src + num);
		}
	}
	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;
}

输出结果

c 复制代码
3 4 5 6 7 6 7 8 9 10

memset

memory set 记忆设置,即内存设置

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

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

code

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

注意点:memset是以字节为单位设置内存值的

输出结果

c 复制代码
xxxxxxworld

memcmp

和strcmp功能其实差不多

c 复制代码
int memcmp ( const void * ptr1, const void * ptr2, size_t num ); 

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

code

c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char arr1[] = "abcdef";
	char arr2[] = "abqwertyuiop";
	int ret = memcmp(arr1, arr2, 2);
	printf("%d\n", ret);

	return 0;
}

输出结果

c 复制代码
0

总结

小编看来,内存函数和字符串函数有很多相似的地方,只不过内存函数针对的内存块罢了。

下一篇更新:数据在内存中的储存

相关推荐
时空自由民.7 分钟前
repo 学习教程
大数据·学习·elasticsearch
1373i27 分钟前
【Python】pytorch数据操作
开发语言·pytorch·python
努力努力再努力wz39 分钟前
【C++进阶系列】:万字详解红黑树(附模拟实现的源码)
java·linux·运维·c语言·开发语言·c++
路弥行至42 分钟前
从0°到180°,STM32玩转MG996R舵机
c语言·数据库·stm32·单片机·嵌入式硬件·mcu·mongodb
枫fengw1 小时前
9.8 C++
开发语言·c++
王璐WL1 小时前
【C语言入门级教学】内存函数
c语言·开发语言·算法
啃啃大瓜1 小时前
python常量变量运算符
开发语言·python·算法
CodingCos1 小时前
【芯片设计-信号完整性 SI 学习 1.1.1 -- Unit Interval,比特周期】
学习·ui·si 比特周期
JCBP_1 小时前
QT(3)
开发语言·汇编·c++·qt·算法
秃顶老男孩.1 小时前
web中的循环遍历
开发语言·前端·javascript