Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

java the hut

Name: Anonymous 2010-10-13 22:41


int i;
char **x = malloc(sizeof(char *) *600);
for(i=0;i<600;i++)
    x[i] = malloc(sizeof(char) * 500);

//do some work


for(i=0;i<600;i++)
    free(Counter[i]); //error
free(Counter);


what did i do wrong?

Name: Anonymous 2010-10-14 1:50

Here is a ``better'' way to construct a dynamic n-by-m array. Assume n and m are of type size_t. Handling of arithmetic wrap-around (if necessary) and malloc failure are omitted for brevity.

type **x = malloc(n * sizeof *x);
x[0] = malloc(n * m * sizeof *x[0]);
for (size_t i = 1; i < n; i++)
  x[i] = x[i-1] + m;

// ...

free(x[0]);
free(x);

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List