Come deallocare memoria in tree prefisso? (ANSI C)
-
04-10-2019 - |
Domanda
ho cercato di deallocare memoria in dict_free funzione (), ma non è così il lavoro e io no no perché. Mi sto perdendo qualcosa? Non riesco a capire, che cosa c'è che non va.
Modifica: Se io chiamo free () in dict_free () mi aspetto di vedere che i punti di puntatore free'd a NULL, ma che non sta accadendo.
Ecco il mio codice:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Dict
{
struct Dict *branches[256];
int index;
}Dict;
void dict_insert_depth(unsigned char*,Dict *,int);
void dict_insert(unsigned char*,Dict *);
void dict_free(Dict *d)
{
if(d!=NULL){
int i;
for(i=0; i<256; i++){
if(d->branches[i] != NULL){
dict_free(d->branches[i]);
free(d->branches[i]);
printf("Is it free?? %s\n",d==NULL?"yes":"no");
}
}
}
}
/**
* Insert word into dictionaR
*/
void dict_insert(unsigned char *w, Dict *d)
{
dict_insert_depth(w,d,0);
}
void dict_insert_depth(unsigned char *w, Dict *d, int depth)
{
if(strlen(w) > depth){
int ch = w[depth];
if(d->branches[ch]==NULL){
d->branches[ch] = malloc(sizeof(struct Dict));
dict_insert_depth(w,d->branches[ch],depth+1);
}else{
dict_insert_depth(w,d->branches[ch],depth+1);
}
}
}
/**
* Check whether a word exists in the dictionary
* @param w Word to be checked
* @param d Full dictionary
* @return If found return 1, otherwise 0
*/
int in_dict(unsigned char *w, Dict *d)
{
return in_dict_depth(w,d,0);
}
int in_dict_depth(unsigned char *w, Dict *d, int depth)
{
if(strlen(w)>depth){
int ch = w[depth];
if(d->branches[ch]){
return in_dict_depth(w, d->branches[ch], depth+1);
}else{
return 0;
}
}else{
return 1;
}
}
Soluzione
Il tuo codice libero guarda bene, tranne che non riuscirà a liberare il nodo radice.
Il test gratis-ness è sbagliato. free
non impostare qualsiasi variabile NULL
. Spesso è una buona idea per farlo in modo esplicito, così si è sicuri di non leggere la memoria già liberata:
free(d->branches[i]);
d->branches[i] = NULL; // clobber pointer to freed memory
Per gestire il problema nodo principale, e probabilmente un po 'più pulito, così, fare questo:
void dict_free(Dict *d)
{
if(d!=NULL){
int i;
for(i=0; i<256; i++){
if(d->branches[i] != NULL){
dict_free(d->branches[i]);
d->branches[i] = NULL;
}
}
free(d);
}
}
Altri suggerimenti
dict_free(d->branches[i]);
free(d->branches[i]);
printf("Is it free?? %s\n",d==NULL?"yes":"no");
Questo controlla d, , ma non modificare d nel ciclo. Dal momento che verificare che d non è nullo sopra, questo non è sempre stampato.
void dict_free(Dict* d) {
if (d) {
for(int i = 0; i < 256; i++) {
if (d->branches[i]) {
dict_free(d->branches[i]);
free(d->branches[i]);
d->branches[i] = 0; // mark this branch as freed
// important if d is reused, and since dict_free doesn't
// free(d), it could be
}
}
}
}
Ho seguito il codice esistente nel non liberare d, , ma si può decidere di cambiare le cose in modo un Dict è sempre assegnato allo stesso modo (ad esempio, aggiungere una funzione dict_new) con dict_free anche liberare il oggetto passato.