adriano
adriano

Reputation: 1

problem with use of free

I create a function to read a file and store its contents on a matrix of char. After store it I want to free the matrix but a execution error occurre. Why it happens? Here is the code:

//the error occurr on function "freearqvetores"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void cpyarqvetores( char*** arqvetores, FILE* varquivo);
void freearqvetores( char*** arqvetores );

int main()
{
    FILE* varquivo;
    varquivo = fopen("vetores.txt", "r");
    char*** arqvetores;
    arqvetores = (char***) malloc(sizeof(char**));
    cpyarqvetores( arqvetores, varquivo);
    printf("%s\n", **(arqvetores + 2));//A test to verify if the function 
//cpyarqvetores works
    //freearqvetores( arqvetores );
    //free(arqvetores);
    fclose( varquivo );
    return 0;
}

void cpyarqvetores( char*** arqvetores, FILE* varquivo){
    char aux[440];
    int strtam;
    int i, j;
    for( i = 0; i < 10; i++){
        fgets( aux, 440, varquivo);
        strtam = strlen( aux );
        *(arqvetores + i) = (char**) malloc(strtam*sizeof(char*));
        if(*(arqvetores + i) == NULL)
            exit(-1);
        for( j = 0; j < strtam; j++){
            *(*(arqvetores + i) + j) = (char*) malloc(sizeof(char));
            if( *(*(arqvetores + i) + j) == NULL)
                exit(-2);
        }
        strcpy( **(arqvetores + i), aux);
    }
}

void freearqvetores( char*** arqvetores ){
    int i, j;
    int strtam;
    for( i = 0; i < 10; i++){
        strtam = strlen( **(arqvetores + i) );
        printf("strtam = %d\n", strtam);
        for( j = 0; j < strtam; j++){
            printf("[%d]\n", j);
//this line is right?
            free(*(*(arqvetores + i) + j));
        }
        free(*(arqvetores + i));
    }
}

The contents of file "vetores" is:

1a 2a 3n 4n 2a 5a 6z 2z 1v 3a 11c 13e 3z 1e 14n 11n 2v 4a 3z 5a
2a 2a 3z 4n 2a 5a 6z 8z 1v 3z 11c 13e 3z 2e 14n 11n 2v 4a 3z 2a
3a 2a 3v 4n 2a 5a 6z 8a 1v 3a 11c 13e 3z 1e 11z 11n 2v 4a 3z 5a
4a 2a 3v 4n 2a 5a 6z 8z 1v 3a 13c 13e 3z 1e 14n 11n 2v 3a 3z 5a
5a 2a 4a 4n 2a 5a 6a 8z 1v 3a 11c 13e 3z 1e 14n 11n 2v 4a 3z 5a
6a 2a 3z 4n 2a 5a 6z 8z 6v 3a 11c 13e 3n 1e 14n 11n 2v 5a 3z 5z
7a 2a 3n 4n 2a 5a 6z 8z 1v 3a 11c 13e 3z 1e 14n 11n 2v 4a 3z 5a
8a 2a 3a 4n 2a 5a 6z 8z 1v 3a 11c 13e 3z 1e 14n 11n 2v 4a 3z 5a
9v 5a 6z 2n 1a 5z 6a 3z 3v 5a 13c 11a 3a 2e 13z 12e 2z 2a 3z 5a
1n 4a 7v 5n 2z 4a 7z 8a 1v 8a 12z 11e 3v 1a 12z 14n 2z 2a 6v 5a

I had to modify the function cpyarqvetores. Here is the code but when I try print the strings of arqvetores a error occurr.

 void cpyarqvetores( char*** arqvetores, FILE* varquivo){
     char aux[440];
     int strtam;
     int i, j;
     *arqvetores = (char**) malloc(10*sizeof(char*));
     if(*(arqvetores) == NULL)
         exit(-1);
     for( i = 0; i < 10; i++){
         fgets( aux, 440, varquivo);
         strtam = strlen( aux );
         *(*arqvetores + i) = (char*) malloc(strtam*sizeof(char));
         if(*(*arqvetores + i) == NULL)
             exit(-1);
         strcpy( *(*arqvetores + i), aux);
     }
     for( i = 0; i < 10; i++){
         printf("%s\n", *(*arqvetores + i));
     }
 }

what is wrong with this code?

Upvotes: 1

Views: 186

Answers (3)

user325117
user325117

Reputation:

You've allocated arqvetores as a pointer to a single pointer-to-pointer to char in main, but then in in cpyarqvetores you iterate over it as if its pointing to 10 pointers-to-pointers-to-char.

Also please #include <string.h> and don't cast the return value of malloc.

Upvotes: 1

Roddy
Roddy

Reputation: 68023

char*** is a bad code smell. Once you learn how to get rid of that, your problems will magically get sidestepped.

I'd start by trying to draw a diagram of the data structures I want and how they link to each other (if at all), and then work out which bits I need to dynamically allocate, and which ones I don't.

Hint1: You only need to dynamically allocate things if you don't know how big they will be at compile time. Hint2: You can have arrays of pointers in C++ as well as arrays of char.

Upvotes: 0

Martin Beckett
Martin Beckett

Reputation: 96109

Apart from the basic design problems.

You need to ensure that you are freeing the same address that you malloced
printf the address returned by malloc for each call and the address you pass to free.

Compare these two lists and it should give you a starting point for where you are going wrong

Upvotes: 1

Related Questions