Click to See Complete Forum and Search --> : char ** allocation


binaryDigit
11-13-2002, 12:34 PM
i feel stupid for asking this question, but i've succeeded in confusing myself.

an array of character pointers:

char **some_array;

if i want to dynamically allocate space for this i have to do this:


char **a;

/* this part really confuses me. it doesn't seem to matter how much space i allocate */
a = malloc(1)

/* allocating a size of 20 to first item in array */
a[0] = (char *) malloc(20);
a[1] = (char *) malloc(30);

/* do stuff with array */

free(a[0]);
free(a[1]);

/* free a ??? */



so what's the right way to allocate and free the necessary space?

Rüpel
11-13-2002, 03:16 PM
char ** a = (char **)malloc(2*sizeof(char*));
a[0] = (char *)malloc(20*sizeof(char));
a[1] = (char *)malloc(30*sizeof(char));
free(a[1]);
a[1]=0;
free(a[0]);
a[0]=0;
free(a);

binaryDigit
11-13-2002, 05:03 PM
Thanks. that makes sense.