1

I'm trying to free mem from 2 dimensional array the same way like in this: [C: Correctly freeing memory of a multi-dimensional array

//initialization
int **tab;
int i,j;
int x_max=5,y_max=7;
tab = calloc(x_max+1, sizeof(int));
for(j = 0; j <=x_max+1; j++)
{
    tab[j] = calloc(y_max+2, sizeof(int));
}

and than:

for (i = 0; i <=x_max+1; i++)
{
    free(tab[i]);
}
free(tab);

This seems to be like in the example in above link, but my program keeps crashing (when I comment freeing mem part everything works good.) Also when I try to debug program line by line everything works. (Debugger finished with status 0)

Community
  • 1
  • 1
sswwqqaa
  • 1,545
  • 3
  • 15
  • 29
  • 6
    You have undefined behavior while *creating* said array (repeated when you're freeing it, but by that point, it does not matter anymore because of earlier UB). You allocate `x_max+1` elements, and yet you access `x_max+2` (`0` to `x_max+1` inclusive). In most cases, you do *not* want a `<=` in a `for` loop in C. – Tim Čas Dec 30 '16 at 13:21
  • 1
    `tab = calloc(x_max+1, sizeof(int)); for(j = 0; j <=x_max+1; j++)` --> `tab = calloc(x_max+1, sizeof(int*)); for(j = 0; j – BLUEPIXY Dec 30 '16 at 13:22
  • 2
    @cokceken Huh? That's not valid C. Array indexing requires an index between the `[` and `]`. – Emil Laine Dec 30 '16 at 13:35
  • @tuple_cat you are right i deleted my comment – cokceken Dec 30 '16 at 13:55
  • There is no 2D array in your code. Something like `int **` is not and cannot point to a 2D array. Use a 2D array. – too honest for this site Dec 30 '16 at 13:59
  • The <= should be replaced by <. As a general rule, always write for loops with <, never with <=. – Malcolm McLean Dec 30 '16 at 13:59

1 Answers1

1

First of all, it's not a 2D array. It's a jagged array.

Your initial allocation is problematic. You want to allocate x_max+1 pointers, not ints. And you are accessing outside of the bounds since you allocated x_max+1 pointers, yet access upto x_max+2 pointers.

It's usually preferred to use the thing itself to allocate memory such as:

tab = calloc(x_max+1, sizeof *tab);

so that you don't need to worry about the type being changed later.

tab = calloc(x_max+1, sizeof(int));
for(j = 0; j <=x_max+1; j++)
{
    tab[j] = calloc(y_max+2, sizeof(int));
}

should be

tab = calloc(x_max+1, sizeof *tab); /* equivalent to: tab = calloc(x_max+1, sizeof(int*)); */
for(j = 0; j < x_max+1; j++)
{
    tab[j] = calloc(y_max+2, sizeof(int));
}

You should also check the return value of calloc() for failures.

P.P
  • 117,907
  • 20
  • 175
  • 238