C: Expanding an array with malloc
Posted
by Mal Ock
on Stack Overflow
See other posts from Stack Overflow
or by Mal Ock
Published on 2010-04-30T22:26:39Z
Indexed on
2010/04/30
22:37 UTC
Read the original article
Hit count: 308
I'm a bit new to malloc and C in general. I wanted to know how I can, if needed, extend the size of an otherwise fixed-size array with malloc.
Example:
#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;
}
- The above example should make clear what I want to accomplish.
(By the way: I need this for an interpreter I write: Work with a fixed amount of variables and in case more are needed, just allocate them dynamically)
© Stack Overflow or respective owner