【发布时间】:2010-04-30 22:26:39
【问题描述】:
总的来说,我对 malloc 和 C 有点陌生。如果需要,我想知道如何使用 malloc 扩展原本固定大小的数组的大小。
例子:
#define SIZE 1000
struct mystruct
{
int a;
int b;
char c;
};
mystruct myarray[ SIZE ];
int myarrayMaxSize = SIZE;
....
if ( i > myarrayMaxSize )
{
// malloc another SIZE (1000) elements
myarrayMaxSize += SIZE;
}
- 上面的例子应该清楚我想要完成什么。
(顺便说一句:我写的解释器需要这个:使用固定数量的变量,如果需要更多,只需动态分配它们)
【问题讨论】: