Memory error trying to free pointers in struct - C -


code:

#include <stdio.h> #include <stdlib.h>  typedef struct nodo{     int valor;     struct nodo* hijo1;     struct nodo* hijo2; }nodo;  typedef nodo* arreglo;  arreglo inic_arr(int longitud); void imp_arr(arreglo a,int longitud); void inic_arbol(arreglo a, int longitud); void free_arbol(arreglo a, int longitud);   int main(){     arreglo a;     int longitud = 10;     = inic_arr(10);     inic_arbol(a,longitud);     imp_arr(a,longitud);     free_arbol(a,longitud);     return 0; }  arreglo inic_arr(int longitud){     int i;     arreglo = (arreglo)calloc(longitud,sizeof(nodo));     for(i = 0; < longitud; i++){         a[i].valor = rand()%10;     }     return a; }  void imp_arr(arreglo a,int longitud){     int i;     for(i = 0;i < longitud; i++){         printf("[%d,",a[i].valor);         if(a[i].hijo1 == null){             printf("-,");         }         else{             printf("%d,",(*(a[i].hijo1)).valor);         }         if(a[i].hijo2 == null){             printf("-]");         }         else{             printf("%d]",(*(a[i].hijo2)).valor);         }      }     printf("\n"); }  void inic_arbol(arreglo a, int longitud){     int i;     for(i = 0; < longitud; i++){         if(2*i + 1 < longitud)             a[i].hijo1 = &a[2*i + 1];         if(2*i + 2 < longitud)             a[i].hijo2 = &a[2*i + 2];     } }   void free_arbol(arreglo a, int longitud){     int i;     for(i = 0; < longitud; i++){         free(a[i].hijo1);         free(a[i].hijo2);     }     free(a); } 

i created arrary of structs (nodo); each have 2 pointers initialised in inic_arbol function. tried print , free them using free_arbol, , shows up:

[3,6,7][6,5,3][7,5,6][5,2,9][3,1,-][5,-,-][6,-,-][2,-,-][9,-,-][1,-,-] *** error in `./a.out': free(): invalid pointer: 0x0000000000a31028 *** ======= backtrace: ========= /lib/x86_64-linux-gnu/libc.so.6(+0x77725)[0x7f3f8adea725] /lib/x86_64-linux-gnu/libc.so.6(+0x7ff4a)[0x7f3f8adf2f4a] /lib/x86_64-linux-gnu/libc.so.6(cfree+0x4c)[0x7f3f8adf6abc] ./a.out[0x40096b] ./a.out[0x400696] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf0)[0x7f3f8ad93830] ./a.out[0x400579] ======= memory map: ======== 00400000-00401000 r-xp 00000000 08:0a 140095                             /home/luis/documentos/programacion/c/a.out 00600000-00601000 r--p 00000000 08:0a 140095                             /home/luis/documentos/programacion/c/a.out 00601000-00602000 rw-p 00001000 08:0a 140095                             /home/luis/documentos/programacion/c/a.out 00a31000-00a52000 rw-p 00000000 00:00 0                                  [heap] 7f3f84000000-7f3f84021000 rw-p 00000000 00:00 0  7f3f84021000-7f3f88000000 ---p 00000000 00:00 0  7f3f8ab5d000-7f3f8ab73000 r-xp 00000000 08:09 135547                     /lib/x86_64-linux-gnu/libgcc_s.so.1 7f3f8ab73000-7f3f8ad72000 ---p 00016000 08:09 135547                     /lib/x86_64-linux-gnu/libgcc_s.so.1 7f3f8ad72000-7f3f8ad73000 rw-p 00015000 08:09 135547                     /lib/x86_64-linux-gnu/libgcc_s.so.1 7f3f8ad73000-7f3f8af33000 r-xp 00000000 08:09 141748                     /lib/x86_64-linux-gnu/libc-2.23.so 7f3f8af33000-7f3f8b132000 ---p 001c0000 08:09 141748                     /lib/x86_64-linux-gnu/libc-2.23.so 7f3f8b132000-7f3f8b136000 r--p 001bf000 08:09 141748                     /lib/x86_64-linux-gnu/libc-2.23.so 7f3f8b136000-7f3f8b138000 rw-p 001c3000 08:09 141748                     /lib/x86_64-linux-gnu/libc-2.23.so 7f3f8b138000-7f3f8b13c000 rw-p 00000000 00:00 0  7f3f8b13c000-7f3f8b162000 r-xp 00000000 08:09 141744                     /lib/x86_64-linux-gnu/ld-2.23.so 7f3f8b340000-7f3f8b343000 rw-p 00000000 00:00 0  7f3f8b35e000-7f3f8b361000 rw-p 00000000 00:00 0  7f3f8b361000-7f3f8b362000 r--p 00025000 08:09 141744                     /lib/x86_64-linux-gnu/ld-2.23.so 7f3f8b362000-7f3f8b363000 rw-p 00026000 08:09 141744                     /lib/x86_64-linux-gnu/ld-2.23.so 7f3f8b363000-7f3f8b364000 rw-p 00000000 00:00 0  7fffddc61000-7fffddc82000 rw-p 00000000 00:00 0                          [stack] 7fffddd39000-7fffddd3b000 r--p 00000000 00:00 0                          [vvar] 7fffddd3b000-7fffddd3d000 r-xp 00000000 00:00 0                          [vdso] ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall] abortado (` 

core' generado)

i have 0 knowledge on tools valgrind if explain me fix i'd appreciate it.

in function

arreglo inic_arr(int longitud){     int i;     arreglo = (arreglo)calloc(longitud,sizeof(nodo));     for(i = 0; < longitud; i++){         a[i].valor = rand()%10;     }     return a; } 

there allocated 1 extent of memory using statement

arreglo = (arreglo)calloc(longitud,sizeof(nodo)); 

you may not free elements of dynamically allocated array

void free_arbol(arreglo a, int longitud){     int i;     for(i = 0; < longitud; i++){         free(a[i].hijo1);         free(a[i].hijo2);     }     free(a); } 

because parts of extent , not allocated dynamically.


Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

serialization - Convert Any type in scala to Array[Byte] and back -

SonarQube Plugin for Jenkins does not find SonarQube Scanner executable -