C语言内存函数

本文详细介绍了C语言中的四个内存操作函数:memcpy用于无重叠区域的内存拷贝,memmove处理有重叠部分的拷贝,memset用于设置内存值,memcmp用于字节内容比较。通过示例展示了这些函数的用法和注意事项。
摘要由CSDN通过智能技术生成

目录

memcpy

memmove

memset

memcmp


memcpy

memcpy:内存拷贝函数

使用内存函数要包含头文件 <string.h>

void * memcpy ( void * destination, const void * source, size_t num );

destination:目标空间

source:源头

num:要拷贝的字节个数

void *:返回的是目标空间的起始地址

使用说明

  1. 函数memcpy从source的位置开始向后复制num个字节的数据到destination指向的内存位置。
  2. 这个函数在遇到 '\0' 的时候并不会停下来。
  3. 如果source和destination指向的空间有任何的重叠,复制的结果都是未定义的。
  4. 因为交换的变量有可能是不同的类型,使用函数接收的要用void*
#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, 5 * sizeof(int));
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

模拟实现

void* my_memcpy(void* dst, const void* src, size_t num)
{
	int i = 0;
	assert(dst && src);
	void* ret = dst;
	while (num--)
	{
		//一个字节一个字节交换
		*(char*)dst = *(char*)src;
		//指针向后走1步,一步走1字节的大小
		dst = (char*)dst + 1;
		src = (char*)src + 1;
	}
	return ret;
}
int main()
{
	int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
	int arr2[10] = { 0 };
	my_memcpy(arr2, arr1, 5 * sizeof(int));
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr2[i]);
	}
	return 0;
}

num为20的时候,程序就会执行20次,最终src会走到arr1[4]最后一个字节,dst会走到arr2[4]最后一个字节的位置

memmove

memmove:拷贝字节,专门用来拷贝目标空间和源头有重叠的部分

void * memmove ( void * destination, const void * source, size_t num );

destination:目标空间

source:源头

num:要拷贝的字节个数

void *:返回的是目标空间的起始地址

将 1 2 3 4 5 拷贝到 3 4 5 6 7的空间去

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

模拟实现memmove

首先我们要分为2种情况考虑

  1. src小于dst (要从后向前拷贝,不然就会把3,4,5给覆盖掉)
  2. src大于dst (要从前向后拷贝)拷贝代码和memcpy一样
void* my_memmove(void* dst, const void* src, size_t num)
{
	assert(dst && src);
	void* ret = dst;
	if (src < dst)
	{
		while (num--)
		{
			*((char*)dst + num) = *((char*)src + num);
		}
	}
	else
	{
		*(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 + 2, arr1, 5 * sizeof(int));
	int i = 0;
	for (i = 0; i < 10; i++)
	{
		printf("%d ", arr1[i]);
	}
	return 0;
}

我这边分析的是src小于dst的情况

因为num最终会一直减减直到0,所以src和dst都会一直向前直到到了一开始的位置才结束

memset

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

void * memset ( void * ptr, int value, size_t num );

int main()
{
	char str[] = "hello world";
	//将字符数组str前5个字符设置成x
	memset(str, 'x', 5);
	printf("%s\n", str);
	return 0;
}

memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );

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


返回值

这个函数其实和strncmp差不多

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值