经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C 语言 » 查看文章
C语言动态内存管理介绍
来源:jb51  时间:2021/12/31 10:41:10  对本文有异议

前言:

简单记录一下,内存管理函数

为什么使用动态内存呢?
简单理解就是可以最大限度调用内存
用多少生成多少,不用时就释放而静止内存不能释放
动态可避免运行大程序导致内存溢出

C 语言为内存的分配和管理提供了几个函数:

头文件:<stdlib.h>

注意:void * 类型表示未确定类型的指针?

1.malloc() 用法

?分配一块大小为 num 的内存空间

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main() {
  5. char name[12];
  6. char *test;
  7. strcpy(name, "KiKiNiNi");
  8. // 动态分配内存
  9. test = (char *) malloc(26 * sizeof(char));
  10. // (void *) malloc(int num) -> num = 26 * sizeof(char)
  11. // void * 表示 未确定类型的指针
  12. // 分配了一块内存空间 大小为 num 存放值是未知的
  13. if (test == NULL) {
  14. fprintf(stderr, "Error - unable to allocate required memory\n");
  15. } else {
  16. strcpy(test, "Maybe just like that!");
  17. }
  18. printf("Name = %s\n", name);
  19. printf("Test: %s\n", test);
  20. return 0;
  21. }
  22. // 运行结果
  23. // Name = KiKiNiNi
  24. // Test: Maybe just like that!

2.calloc() 用法

?分配 num 个长度为 size 的连续空间

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main() {
  5. char name[12];
  6. char *test;
  7. strcpy(name, "KiKiNiNi");
  8. // 动态分配内存
  9. test = (void *) calloc(26, sizeof(char));
  10. // (void *) calloc(int num, int size) -> num = 26 / size = sizeof(char)
  11. // void * 表示 未确定类型的指针
  12. // 分配了 num 个 大小为 size 的连续空间 存放值初始化为 0
  13. if (test == NULL) {
  14. fprintf(stderr, "Error - unable to allocate required memory\n");
  15. } else {
  16. strcpy(test, "Maybe just like that!");
  17. }
  18. printf("Name = %s\n", name);
  19. printf("Test: %s\n", test);
  20. return 0;
  21. }
  22. // 运行结果
  23. // Name = KiKiNiNi
  24. // Test: Maybe just like that!

3.realloc() 与 free() 用法

重新调整内存的大小和释放内存

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int main() {
  5. char name[12];
  6. char *test;
  7. strcpy(name, "KiKiNiNi");
  8. // 动态分配内存
  9. test = (char *) malloc(26 * sizeof(char));
  10. // (void *) malloc(int num) -> num = 26 * sizeof(char)
  11. // void * 表示 未确定类型的指针
  12. // 分配了一块内存空间 大小为 num 存放值是未知的
  13. if (test == NULL) {
  14. fprintf(stderr, "Error - unable to allocate required memory\n");
  15. } else {
  16. strcpy(test, "Maybe just like that!");
  17. }
  18. /* 假设您想要存储更大的描述信息 */
  19. test = (char *) realloc(test, 100 * sizeof(char));
  20. if (test == NULL) {
  21. fprintf(stderr, "Error - unable to allocate required memory\n");
  22. } else {
  23. strcat(test, " It's a habit to love her.");
  24. }
  25. printf("Name = %s\n", name);
  26. printf("Test: %s\n", test);
  27. // 释放 test 内存空间
  28. free(test);
  29. return 0;
  30. }
  31. // 运行结果
  32. // Name = KiKiNiNi
  33. // Test: Maybe just like that! It's a habit to love her.

到此这篇关于C语言动态内存管理介绍的文章就介绍到这了,更多相关C语言动态内存内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号