Mail Archives: djgpp/1998/07/18/10:31:35
Martin Ambuhl wrote:
>
> John Meyer wrote in message <35b0151a DOT 9633555 AT news DOT rmi DOT net>...
> |On Mon, 13 Jul 1998 22:28:54 -0700, Nate Eldredge <nate AT cartsys DOT com>
> |wrote:
> |
> |>int a[3000][3000];
> |
> |
> |Okay, but how do you make an array dynamic?
> ===========
> #include <stdlib.h>
> int main(void)
> {
> int *a;
> if (!(a = malloc(9000000))) exit(EXIT_FAILURE);
> /* do stuff */
> free(a);
> return 0;
> }
>
> Martin Ambuhl (mambuhl AT tiac DOT net)
> /* Newsgroup posts also e-mailed */
An alternative would be:
#include <stdlib.h>
int main(void)
{
int **a;
int i;
a = (int **) malloc(3000*sizeof(int));
for(i=0;i<3000;i++)
a[i] = (int *) malloc(3000*sizeof(int);
....
/* to free memory */
for(i=0;i<3000;i++)
free(a[i]);
free(a);
}
- Raw text -