-6

first, sorry for my bad english jeje. I don't speak it.

I'm having a problem when using free() function to free memory, this is what I have :

Using free() function in C

I'm using codeblocks on Windows 7. I have around 3 years using dinamic memory allocation and this is the first time I get this problem. There are people that say that the problem is gcc compiler

equal Directions

Directions are equal in and out the function, so I don't think I'm trying to free an invalid pointer

Community
  • 1
  • 1
  • 4
    Do not post your code as image. Please post your code as text directly in your question. Also please don't forget to post a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – MikeCAT Jun 01 '16 at 16:35
  • 3
    They say [you shouldn't cast the result of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Jun 01 '16 at 16:36
  • What's the question? – Wayne Booth Jun 01 '16 at 16:36
  • Checking if `M == NULL` after assigning some value to `*M` isn't good. See [EXP34-C. Do not dereference null pointers - CERT C Coding Standard - CERT Secure Coding Standards](https://www.securecoding.cert.org/confluence/display/c/EXP34-C.+Do+not+dereference+null+pointers) – MikeCAT Jun 01 '16 at 16:37
  • I am feeling like opened Instagram... – Eugene Sh. Jun 01 '16 at 16:40

3 Answers3

1

Your for loop should be using n not m on your second round of allocation.

for (i=0; i < n; i++) // You're using m here: use n
{
    ...
    malloc(m...) // Here m is correct
noelicus
  • 14,468
  • 3
  • 92
  • 111
0

The errors in the code is:

  • M == NULL is not a valid way to check if malloc is succeeded. You should use (*M) == NULL because the value is assigned to (*M), not M.
  • The loop condition in ReservarMemoriaMatriz i<=m seems wrong. It should be i<=n.
  • You forgot to free (*M)[n] in LiberarMemoriaMatriz.

Corrected code:

void LiberarMemoriaMatriz(double*** M,int n){
    int i = 0;
    if(M == NULL) return;
    for(i=0; i<=n; i++){
        free((*M)[i]);
    }
    free(*M);
}

int ReservarMemoriaMatriz(double*** M, int n, int ,){
    int i = 0;
    if(M == NULL) return 0;
    (*M) = malloc((n+1)*sizeof(double*));
    if((*M) == NULL){
        return 0;
    }
    for(i = 0; i<=n; i++){
        (*M)[i] = malloc(m*sizeof(double));
        if((*M)[i] == NULL){
            /* free what is allocated before failing */
            for(i--; i>=0; i--) free((*M)[i]);
            free(*M);
            return 0;
        }
    }
    return 1;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Lines 2-4 of ReservarMemoriaMatriz are still wrong, Presumably the caller is passing `&M` as the first parameter, and that can never be NULL. so the second line is meaningless. Line 4 should check *M for NULL, not M. And assuming the caller also passes &M into LiberarMemoriaMatriz, the same applies there, too. – FredK Jun 01 '16 at 17:00
0

I was looking line-by-line and finally found the mistake:

I like what you do that says:

free what is allocated before failing

Another thing that I changed is when allocating memory I use:

(*M) = (double**)malloc(n * sizeof(double*));

and the loop condition, i < n, and everything is working fine.

Jamal
  • 763
  • 7
  • 22
  • 32