内存函数memcpy和memmove

memcpy 内存拷贝

函数原型:void * memcpy(void * destination , void * source, size_t num);

  1. 函数mencpy从source的位置开始向后复制num个字符的数据到destinaton的内存位置
  2. 这个函数遇到'\0'并不会停下来
  3. 如果source和destination有任何的重叠,复制的结果都是未定义的
  4. 用于两块独立的内存之间的拷贝
javasScript 复制代码
int main()
{
    int arr1[] = {1, 2, 3, 4, 5, 6, 7};
    int arr2[10] = {0};
    memcpy(arr2, arr1, 28);   //最后一个参数的单位是byte
}

模拟实现memcpy函数,如下:

javaScript 复制代码
void* my_memcpy(void* dest, const void* src, size_t num)
{
	assert(dest && src);
	void* p = dest;
	while (num--)
	{
		*(char*)dest = *(char*)src;
		dest =(char *)dest + 1;
		src = (char*)src + 1;
	}
	return p;
}

memmove 内存移动

函数原型:void * memmove ( void * destination, const void * source, size_t num );

  1. Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.
  2. 用于同一块内存的覆盖拷贝
javaScript 复制代码
int main ()
{
  char str[] = "memmove can be very useful......";
  memmove (str+20,str+15,11);  //用于同一块内存的覆盖拷贝
  puts (str);
  return 0;
}

输出

javaScript 复制代码
memmove can be very very useful.

memcmp 比较内存中两个数据的大小

函数原型:int memcmp(const void *ptr1, const void * ptr2, size_t num);

  1. 比较从ptr1和ptr2指针开始的num个字节
  2. 返回值如下:
return value indicates
< 0 the first byte that does not match in both memory blocks has a lower value in ptr1 than in ptr2 (if evaluated as unsigned char values)
0 the contents of both memory blocks are equal
> 0 the first byte that does not match in both memory blocks has a greater value in ptr1 than in ptr2 (if evaluated as unsigned char values)
javaScript 复制代码
int main()
{
    int arr1[] = {1, 2, 3, 4, 5};
    int arr2[] = {1, 3, 2};
    int ret = memcmp(arr1, arr2, 12);   //比较是按一个字节一个字节的比较
    printf("%d\n", ret);
}

输出

javaScript 复制代码
-1

memset 内存设置指定的值

函数原型:void * memset(void *ptr, int value, size_t num);

  1. Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
  2. 按字节设置。

用法一:设置字符的值

javaScript 复制代码
int main ()
{
  char str[] = "almost every programmer should know memset!";
  memset (str,'-',6);
  puts (str);
  return 0;
}

输出

javaScript 复制代码
------ every programmer should know memset!

用法二,设置int型的值

javaScript 复制代码
int main()
{
	int arr[] = { 1, 2, 3, 4, 5 };
	int i = 0;
	memset(arr, 1, 20);
	for (i = 0; i < 5; i++)
	{
		printf("%d ", arr[i]);
	}
}

输出

javaScript 复制代码
16843009
16843009
16843009
16843009
16843009

由以上结果分析,由于memset是按字节一个一个的来设置value,所以int型的变量,就是被设置为:0x01010101 即为16843009;

随意meset对int型的通常用法是清零:memset(arr, 0 ,20)

相关推荐
凌肖战1 小时前
力扣网C语言编程题:在数组中查找目标值位置之二分查找法
c语言·算法·leetcode
BreezeJuvenile2 小时前
数据结构与算法分析课设:一元多项式求值
c语言·课程设计·数据结构与算法分析·一元多项式计算
悲伤小伞3 小时前
linux_git的使用
linux·c语言·c++·git
气质、小青年!4 小时前
【排序算法】
c语言·数据结构
智者知已应修善业4 小时前
【51单片机节日彩灯控制器设计】2022-6-11
c语言·经验分享·笔记·单片机·嵌入式硬件·51单片机
开-悟5 小时前
嵌入式编程-使用AI查找BUG的启发
c语言·人工智能·嵌入式硬件·bug
Natsume171010 小时前
嵌入式开发:GPIO、UART、SPI、I2C 驱动开发详解与实战案例
c语言·驱动开发·stm32·嵌入式硬件·mcu·架构·github
shaun200110 小时前
华为c编程规范
c语言
MeshddY11 小时前
(超详细)数据库项目初体验:使用C语言连接数据库完成短地址服务(本地运行版)
c语言·数据库·单片机
森焱森11 小时前
无人机三轴稳定化控制(1)____飞机的稳定控制逻辑
c语言·单片机·算法·无人机