C进阶:内存函数memcpy, memmove, memcmp

简介: C进阶:内存函数memcpy, memmove, memcmp

一.memcpy

1.功能

拷贝内存块,将字节的值从指向的位置直接复制到目标指向的内存块。

2.库函数定义

3.实例

1. /* memcpy example */
2. #include <stdio.h>
3. #include <string.h>
4. 
5. struct {
6. char name[40];
7. int age;
8. } person, person_copy;
9. 
10. int main ()
11. {
12. char myname[] = "Pierre de Fermat";
13. 
14. /* using memcpy to copy string: */
15. memcpy ( person.name, myname, strlen(myname)+1 );
16.   person.age = 46;
17. 
18. /* using memcpy to copy structure: */
19. memcpy ( &person_copy, &person, sizeof(person) );
20. 
21. printf ("person_copy: %s, %d \n", person_copy.name, person_copy.age );
22. 
23. return 0;
24. }

4.模拟实现

我们仿照库函数的定义来设计参数和返回值;

因为参数的类型是  void *  ,所以不能直接解引用,和 qsort 函数那里一样,我们采用强制类型转换成 char *  ,然后解引用;

qsort函数:http://t.csdn.cn/iLwjY

具体代码:

1. void* my_memcpy(void* dest, const void* src, size_t num)
2. {
3.  assert(dest && src);
4.  void* ret = dest;
5.  while (num--)
6.  {
7.    //*((char*)dest)++ = *((char*)src)++;   //这种写法并不是所有编译器都行的
8.    *(char*)dest = *(char*)src;
9.    dest = (char*)dest + 1;
10.     src = (char*)src + 1;
11.   }
12.   return ret;
13. }

C 语言定义中, memcpy  ,只需要拷贝不重叠的部分,但在 vs 编译器中,memcpy 也能拷贝重叠的部分,功能和 memmove 类似了;


二.memmove

1.功能

移动内存块,将字节的值从指向的位置复制到目标指向的内存块。

2.库函数定义

3.实例

1. #include <stdio.h>
2. #include <string.h>
3. 
4. int main ()
5. {
6. char str[] = "memmove can be very useful......";
7. memmove (str+20,str+15,11);
8. puts (str);
9. return 0;
10. }

4.模拟实现

请看下图:

具体代码:

1. void* my_memmove(void* dest, const void* src, size_t num)
2. {
3.  assert(dest && src);
4.  void* ret = dest;
5.  if (dest > src)
6.  {
7.    //从后往前
8.    while (num--)
9.    {
10.       *((char*)dest + num) = *((char*)src + num);
11.     }
12.   }
13.   else if (dest < src)
14.   {
15.     //从前往后
16.     while (num--)
17.     {
18.       *(char*)dest = *(char*)src;
19.       dest = (char*)dest + 1;
20.       src = (char*)src + 1;
21.     }
22.   }
23.   return ret;
24. }

三.memcmp

1.功能

比较两个内存块;

2.库函数定义

3.实例

1. #include <stdio.h>
2. #include <string.h>
3. 
4. int main ()
5. {
6. char buffer1[] = "DWgaOtP12df0";
7. char buffer2[] = "DWGAOTP12DF0";
8. 
9. int n;
10. 
11.   n=memcmp ( buffer1, buffer2, sizeof(buffer1) );
12. 
13. if (n>0) printf ("'%s' is greater than '%s'.\n",buffer1,buffer2);
14. else if (n<0) printf ("'%s' is less than '%s'.\n",buffer1,buffer2);
15. else printf ("'%s' is the same as '%s'.\n",buffer1,buffer2);
16. 
17. return 0;
18. }

运行结果:

 


以上的内存函数都定义在头文件 <string.h> 中;


🤖👻本篇文章到这里就结束了,如有错误或是建议,欢迎小伙伴们指出;🐬🕊️

😆🤩希望小伙伴们可以支持支持博主,你们的支持对我很重要;🥰😁

🐲👻谢谢你的阅读;😄😀


目录
相关文章
|
18天前
|
程序员 C语言
C语言库函数 — 内存函数(含模拟实现内存函数)
C语言库函数 — 内存函数(含模拟实现内存函数)
29 0
|
3天前
|
C语言
C语言:内存函数(memcpy memmove memset memcmp使用)
C语言:内存函数(memcpy memmove memset memcmp使用)
|
6天前
|
编译器 C语言
字符串与内存函数
字符串与内存函数
19 0
|
29天前
|
编译器 C语言 C++
【C语言】calloc()函数详解(动态内存开辟函数)
【C语言】calloc()函数详解(动态内存开辟函数)
25 0
|
1月前
|
存储 JSON 监控
Higress Controller**不是将配置信息推送到Istio的内存存储里面的**。
【2月更文挑战第30天】Higress Controller**不是将配置信息推送到Istio的内存存储里面的**。
14 1
|
1月前
|
存储 C语言
C语言--------数据在内存中的存储
C语言--------数据在内存中的存储
26 0
|
2天前
|
存储 算法
【三种方法】求一个整数存储在内存中二进制中的1的个数附两道课外练习题
【三种方法】求一个整数存储在内存中二进制中的1的个数附两道课外练习题
7 0
|
2天前
|
存储
数据在内存中的存储之整数存储
数据在内存中的存储之整数存储
8 0
|
9天前
|
存储 NoSQL Oracle
Oracle 12c的内存列存储:数据的“闪电侠”
【4月更文挑战第19天】Oracle 12c的内存列存储以超高速度革新数据处理,结合列存储与内存技术,实现快速查询与压缩。它支持向量化查询和并行处理,提升效率,但需合理配置以平衡系统资源。作为数据管理员,应善用此功能,适应业务需求和技术发展。
|
19天前
|
存储 C语言
数据在内存中的存储2
数据在内存中的存储2