Release memory from the node of a TRIE in C

1

I am with a problem for a project that I am doing in the subject Organization of Computers. In the program I have to create a TRIE structure that reads words from a file and counts the number of occurrences of each word. The TRIE nodes have an ordered list of children, a label that is the character and a pointer to the parent node. The list and the ordered list are already implemented and eliminate the elements and free the memory of their nodes without problems. The problem is when deleting elements of the TRIE, it eliminates them but it throws me an error " invalid next size fast () " in the free () method.

The structure of the TRIE nodes is as follows

typedef struct Nodo{
  char rotulo;
  unsigned int contador;
  struct * nodo padre;
  TListaOrdenada hijos;
} * TNodo

The method to create each node is as follows. The list of children is created outside of this function, by inserting an element:

TNodo tr_crear_nodo(char c){
    TNodo nodo = (TNodo) malloc(sizeof(TNodo));
    nodo->contador = 0;
    nodo->rotulo = c;
    return nodo;
}

The method to eliminate is tr_delete () that receives a TRIE and the word. Returns another recursive method that finds the word and the leaf node. If you find it, call the method tr_delete_nodes () that eliminates the nodes from bottom to top.

int tr_eliminar(TTrie tr, char *str){
    if(tr == NULL)
        exit(TRI_NO_INI);
    return tr_eliminar_aux(tr, str, tr->raiz);
}

int tr_eliminar_aux(TTrie tr, char *str, TNodo root){
    if(*str == '
    int tr_destruir_nodo(TNodo nodo){
        free(nodo->hijos);
        free(nodo);
    }
'){ if(root->contador > 0) return tr_eliminar_nodos(tr, root); else return STR_NO_PER; }else{ if(root == NULL) return STR_NO_PER; TListaOrdenada lista_hijos = root->hijos; if(lo_size(lista_hijos) == 0) return STR_NO_PER; TPosicion pos = lo_primera(root->hijos); TNodo nodo_pos = pos->elemento; while(*str != nodo_pos->rotulo){ pos = lo_siguiente(root->hijos, pos); if(pos == POS_NULA) return STR_NO_PER; else nodo_pos = pos->elemento; } str++; return tr_eliminar_aux(tr, str, nodo_pos); } } int tr_eliminar_nodos(TTrie tr, TNodo root){ if(lo_size(root->hijos) == 0){ if(root == tr->raiz) return TRUE; TNodo padre = root->padre; TPosicion pos = lo_primera(padre->hijos); TNodo nodo_pos = pos->elemento; while(nodo_pos->rotulo != root->rotulo){ pos = lo_siguiente(padre->hijos, pos); nodo_pos = pos->elemento; } tr_destruir_nodo(root); lo_eliminar(padre->hijos, pos); if(padre->contador > 0) return TRUE; return tr_eliminar_nodos(tr, padre); }else return TRUE; }

The method works correctly to eliminate the nodes, the problem is in the last lines of the function tr_delete_nodes () when using the method tr_destruir_nodo (Node node) that releases the memory used by the list of children and the node.

typedef void * TElemento;
typedef struct celda * TPosicion;

typedef struct celda{
    TElemento elemento;
    struct celda * proxima_celda;
} * TCelda;

typedef struct Lista{
    unsigned int cantidad_elementos;
    TCelda primer_celda;
} * TLista;

void l_destruir_nodo(TPosicion nodo){
    free(nodo);
    nodo->elemento = ELE_NULO;
    nodo = NULL;
}

TPosicion crear_nodo(TElemento elem){
    TPosicion nodo = (TPosicion) malloc(sizeof(TPosicion));
    nodo->elemento = elem;
    nodo->proxima_celda = POS_NULA;
    return nodo;
}

The problem I think may also be in the form that the list removes its elements, so I add its structures and the method to create and delete each node.

typedef struct Nodo{
  char rotulo;
  unsigned int contador;
  struct * nodo padre;
  TListaOrdenada hijos;
} * TNodo
    
asked by Dylan Barbona 05.10.2017 в 19:10
source

1 answer

1

What you ask, without a minimal, complete and verifiable example, is quite difficult.

I just tell you one thing that is wrong:

void l_destruir_nodo(TPosicion nodo){
  free(nodo);
  nodo->elemento = ELE_NULO;
  nodo = NULL;
}

You release it correctly; so nodo->elemento = ELE_NULO is absolutely futile. You can save it perfectly.

But ... then you do nodo = NULL . That does not help at all, since you only alter the value of nodo within the function ; at the calling point, nodo remains unchanged.

You should investigate from there.

    
answered by 05.10.2017 в 19:22