Note that there are some explanatory texts on larger screens.

plurals
  1. POproblem with use of free
    text
    copied!<p>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:</p> <pre><code>//the error occurr on function "freearqvetores" #include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; #include &lt;string.h&gt; 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 &lt; 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 &lt; 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 &lt; 10; i++){ strtam = strlen( **(arqvetores + i) ); printf("strtam = %d\n", strtam); for( j = 0; j &lt; strtam; j++){ printf("[%d]\n", j); //this line is right? free(*(*(arqvetores + i) + j)); } free(*(arqvetores + i)); } } </code></pre> <p>The contents of file "vetores" is:</p> <pre><code>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 </code></pre> <p>I had to modify the function cpyarqvetores. Here is the code but when I try print the strings of arqvetores a error occurr.</p> <pre><code> 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 &lt; 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 &lt; 10; i++){ printf("%s\n", *(*arqvetores + i)); } } </code></pre> <p>what is wrong with this code?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload