本文是对于动态内存管理知识后续的补充,以及加深对其的理解。对于动态内存管理涉及的大部分知识在这篇文章中 ---- 【C进阶】 动态内存管理_Dream_Chaser~的博客-CSDN博客
本文涉及的知识内容主要在两方面:
- 简单解析C/C++程序的内存开辟
- 分析柔性数组的知识点
目录
- 实际上普通的局部变量是在栈区分配空间的,栈区的特点是在上面创建的变量出了作用域就销毁。
- 但是被static修饰的变量存放在数据段(静态区),数据段的特点是在上面创建的变量,直到程序结束才销毁,所以生命周期变长。
柔性数组(Flexible Array)是一种在编程语言中用于表示可变长度的数组的数据结构。它允许在声明数组时不指定数组的长度,而是在运行时根据需要动态分配内存空间。
柔性数组最常见的应用是在C语言中。在C语言中,柔性数组是一种特殊的结构体成员,其长度可以在结构体实例化之前或之后进行动态调整。
C99 中,结构中的最后一个元素允许是未知大小的数组,这就叫做『柔性数组』成员。
啥意思呢,用代码说话
在vs编译器环境下,以下两种写法均支持
第一种写法(使用空方括号[ ])是更常见和更符合标准的写法,可以在大多数编译器环境下使用。
- struct S
- {
- int n;
- char c;
- int arr[];//柔性数组成员
- };
第二种写法(指定大小为0)在某些特定的编译器(vs)扩展中可能有效,但不具有通用性和可移植性。
- struct S
- {
- int n;
- char c;
- int arr[0];//柔性数组成员(指定大小)
- };
1️⃣结构中的柔性数组成员前面必须至少一个其他成员。
- typedef struct st_type
- {
- int i;//必须至少一个其他成员
- int a[0];//👈柔性数组成员
- }type_a;
错误写法:
- struct SA
- {
- int arr[];//柔性数组成员
- };
2️⃣sizeof 返回的这种结构大小不包括柔性数组的内存

- struct S
- {
- int n;
- char c;
- int arr[];//柔性数组成员
- };
- int main()
- {
- printf("%d", sizeof(struct S));
- }
🚩8
3️⃣包含柔性数组成员的结构用malloc ()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。
- #include<stdio.h>
- #include<string.h>
- #include<errno.h>
- #include<stdlib.h>
- int main()
- {
- //arr需要开辟的空间是10个int
- // n与c需要开辟的内存 arr数组需要开辟的内存空间
- // 8 40
- struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
-
-
- return 0;
- }
图解:

代码实现🎯
- #include<stdio.h>
- #include<string.h>
- #include<errno.h>
- #include<stdlib.h>
-
- //柔性数组
- struct S
- {
- int n;
- char c;
- int arr[];//柔性数组成员
- };
-
- int main()
- {
- struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
- if (ps == NULL)
- {
- printf("%s\n",strerror(errno));
- return 1;
- }
- //使用
- ps->n = 100;
- ps->c = 'w';
- int i = 0;
- for ( i = 0; i < 10; i++)
- {
- ps->arr[i] = i;
- }
-
- for (i = 0; i < 10; i++)
- {
- printf("%d\n", ps->arr[i]);
- }
- //调整arr数组的大小(注意这是重新改变大小,不是说在原来空间后面增加,比如说原来是48,那么现在就是88)
- struct S* ptr = (struct S*)realloc(ps, sizeof(struct S) + 20 * sizeof(int));
- if (ptr == NULL)
- {
- printf("%s\n,sterror(error)");
- return 1;
- }
- else
- {
- ps = ptr;
- }
- //再次使用
- //....
-
- //释放
- free(ps);
- ps = NULL;
-
- printf("%d\n", sizeof(struct S));
-
- return 0;
- }
调试一下,看看空间大小如何
malloc的空间,58-30=28(16进制),换成十进制刚好为40,刚好是10int的字节大小
方案一:柔性数组的方案
- #include<stdio.h>
- #include<string.h>
- #include<errno.h>
- #include<stdlib.h>
-
- //柔性数组
- struct S
- {
- int n;
- char c;
- int arr[];//柔性数组成员
- };
-
- int main()
- {
- struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
- if (ps == NULL)
- {
- printf("%s\n",strerror(errno));
- return 1;
- }
- //使用
- ps->n = 100;
- ps->c = 'w';
- int i = 0;
- for ( i = 0; i < 10; i++)
- {
- ps->arr[i] = i;
- }
-
- for (i = 0; i < 10; i++)
- {
- printf("%d\n", ps->arr[i]);
- }
- //调整arr数组的大小(注意这是重新改变大小,不是说在原来空间后面增加,比如说原来是48,那么现在就是88)
- struct S* ptr = (struct S*)realloc(ps, sizeof(struct S) + 20 * sizeof(int));
- if (ptr == NULL)
- {
- printf("%s\n,sterror(error)");
- return 1;
- }
- else
- {
- ps = ptr;
- }
- //再次使用
- //....
-
- //释放
- free(ps);
- ps = NULL;
-
- printf("%d\n", sizeof(struct S));
-
- return 0;
- }
描述:
malloc 1次 ,free 1次
方案二:结构中指针方案
定义一个指针变量指向一块新的区域,像下面这样
图解:
代码实现✨
- struct S
- {
- int n;
- char c;
- int* arr;
- };
-
- int main()
- {
- struct S* ps = (struct S*)malloc(sizeof(struct S));
- if (ps == NULL)
- {
- perror("malloc");
- return 1;
- }
- int* ptr = (int*)malloc(10 * sizeof(int));
- if (ptr == NULL)
- {
- perror("malloc2");
- return 1;
- }
- else
- {
- ps->arr = ptr;
- }
- //使用
- ps->n = 100;
- ps->c = 'w';
- int i = 0;
- for (i = 0; i < 10; i++)
- {
- ps->arr[i] = i;
- }
- //打印
- for (i = 0; i < 10; i++)
- {
- printf("%d ",ps->arr[i]);
- }
- //扩容 - 调整arr的大小
- ptr = realloc(ps->arr,20*sizeof(int));
- if (ptr == NULL)
- {
- perror("realloc");
- return 1;
- }
- else
- {
- ps->arr = ptr;
- }
- //使用
-
- //释放
- free(ps->arr);
- ps->arr = NULL;
- free(ps);
- ps = NULL;
-
- return 0;
- }
描述:
malloc 2次,free 2次
上面的方案一和方案二谁的优势更优呢,显然是方案一。
个人的理解:
从写代码的方面来说,malloc越多,free的越多,空间的维护难度就更高,所以
方案一实现起来更加简单,空间维护更加简单,容易维护空间,不易出错
方案二来说,一旦忘记free一次的话,可能会导致内存泄漏等问题,所以维护难度加大,容易出错
还有区别就是:
在堆区上申请内存的话,每一次malloc申请的空间,第二次malloc申请的空间跟第一次申请的空间在地址上不一定是连续的,随机性很高,随着malloc申请的数量越多,那么在内存和内存之间留下的空隙就会越多,这种空隙我们叫做为内存碎片
因为这种内存碎片空间大小比较小一些,那么未来可能被利用到的概率就会比较低一些,所以说,内存碎片越多,那么内存利用率就会越低
总结
①方案一:malloc次数少,内存碎片就会较少,内存的使用率就较高一些
②方案二:malloc次数多,内存碎片就会增多,内存的使用率就下降了
如果我们的代码是在一个给别人用的函数中,你在里面做了二次内存分配,并把整个结构体返回给用户。用户调用free可以释放结构体,但是用户并不知道这个结构体内的成员也需要free,所以你不能指望用户来发现这个事。所以, 如果我们把结构体的内存以及其成员要的内存一次性分配好了,并返回给用户一个结构体指针,用户做一次free就可以把所有的内存也给释放掉。
连续的内存有益于提高访问速度,也有益于减少内存碎片。(其实,我个人觉得也没多高了,反正你跑不了要用做偏移量的加法来寻址)
本文结束,如有错误,欢迎指正,感谢支持!