我对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;
}
Run Code Online (Sandbox Code Playgroud)
(顺便说一句:我需要这个用于我编写的解释器:使用固定数量的变量,如果需要更多变量,只需动态分配它们)