Wie Speicher in Präfixbaums freizugeben? (ANSI C)
-
04-10-2019 - |
Frage
Ich habe versucht, ausplanen Speicher in dict_free () Funktion, aber es funktioniert nicht und ich weiß nicht, warum nicht. Bin ich etwas fehlt? Kann nicht herausfinden, was los ist.
Edit: Wenn ich rufe free () in dict_free () ich, dass free'd Zeiger auf NULL zu erwarten, aber das ist nicht passiert.
Hier ist mein Code:
#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;
}
}
Lösung
Ihr freier Code sieht gut aus, außer es den Wurzelknoten befreien wird fehlschlagen.
Ihr Test kostenlos-ness ist falsch. free
wird keine Variable NULL
gesetzt. Oft ist es eine gute Idee, dass explizit zu tun, damit Sie sicher sind, nicht bereits freigegebenen Speicher zu lesen:
free(d->branches[i]);
d->branches[i] = NULL; // clobber pointer to freed memory
den Wurzelknoten Problem zu behandeln, und wahrscheinlich etwas sauberer als auch, dies zu tun:
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);
}
}
Andere Tipps
dict_free(d->branches[i]);
free(d->branches[i]);
printf("Is it free?? %s\n",d==NULL?"yes":"no");
Diese Kontrollen d , aber sie nicht ändern d in der Schleife. Da Sie, dass der Check d ist null nicht oben, dies immer nicht druckt.
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
}
}
}
}
Ich habe Ihren vorhandenen Code gefolgt in nicht befreien d , aber Sie ändern wollen Dinge können so ein Dict immer die gleiche Art und Weise zugeordnet ist (zB eine dict_new Funktion hinzufügen) mit dict_free auch der Befreiung gebene Objekt.