004
09.09.2003, 17:52 Uhr
~xodi
Gast
|
hast recht, ich werde die mallocs in Zukunft alle prüfen.
Noch ne Frage zu Speicherbereichen:
wenn ich mit
C++: |
dl = (struct dirnames **) malloc(sizeof(struct dirnames *) * (ne = 400));
|
ein Array der Strukturen definier, wie bekomm ich das nachher wieder weg?
folgendes Beispiel:
C++: |
struct dirnames **read_dir(char *dir, float *thisdirsize, int *thisdirfiles, char *tname) { char path[2048]; struct dirnames **dl; struct dirent *ent; struct stat lst; DIR *d; int ne, p = 0, bigsize = 1024 * atoi(getvalue(tname, "bigsize")), olddate = time((time_t *)NULL) - atoi(getvalue(tname, "olddate"))*3600*24;
if(debug > 0) printf("read_dir %d\n", debug++);
if ((d=opendir(dir)) == NULL) { fprintf(stderr,"%s: [error opening directory: %s]\n",do_date(time((time_t *)NULL), TRUE), dir); fprintf(stdout,"%s: [error opening directory: %s]\n",do_date(time((time_t *)NULL), TRUE), dir); return NULL; }
dl = (struct dirnames **) malloc(sizeof(struct dirnames *) * (ne = 400));
while((ent = (struct dirent *)readdir(d))) { if ((!strcmp("..",ent->d_name)) || (!strcmp(".",ent->d_name)) || (!strncmp(".snapshot",ent->d_name, 9))) continue;
snprintf(path,strlen(dir)+strlen(ent->d_name)+3,"%s/%s",dir,ent->d_name);
if (lstat(path,&lst) < 0 || ((lst.st_mode & S_IFMT) == S_IFLNK)) { (*thisdirfiles)++; continue; } if((lst.st_mode & S_IFMT) == S_IFDIR) { dl[p] = (struct dirnames*) malloc(sizeof(struct dirnames)); dl[p++]->name = scopy(ent->d_name); if(p>ne) dl = (struct dirnames **) realloc(dl,sizeof(struct dirnames *) * (ne += 100)); continue; } (*thisdirsize) += lst.st_size; (*thisdirfiles)++; if((float)lst.st_size > (float)bigsize) // Is it big? -> Remember it insert(((float)(lst.st_size)/1024), do_date(lst.st_mtime, TRUE),dir, 0, ent->d_name, 1); if(olddate > lst.st_mtime) // Is it old? -> Remember it insert(((float)(lst.st_size)/1024), do_date(lst.st_mtime, TRUE),dir, 0, ent->d_name, 2); if(!(*thisdirfiles % 10000) && communicative > 0) fprintf(stdout, "processing \"%s/\", I've checked %d files by now...\n", dir, *thisdirfiles);
} closedir(d); dl[p] = NULL; if(debug > 0) printf("read_dir Ende\n"); return dl; }
|
Da wird also der Bereich allokiert. In der folgenden Funktion wird der Block verarbeitet:
C++: |
float process_dir(char *directory, char *tname, int *filestotal, float *sizetotal, unsigned int level) { struct dirnames **dir; char path[2048]; float dsize = 0; int dfiles = *filestotal; dir = read_dir(directory, &dsize, filestotal, tname); if(debug > 0) printf("process_dir %d\n", debug++); if(dir) { while(*dir) { if(strlen(directory) == 1) snprintf(path,strlen((*dir)->name)+3,"/%s",(*dir)->name); else snprintf(path,strlen(directory)+strlen((*dir)->name)+2,"%s/%s",directory,(*dir)->name); dsize += process_dir(path,tname, filestotal, sizetotal, (level + 1)); free((*dir)->name); free(*(dir++)); } }
insert(((float)dsize)/1024, "", directory, ((*filestotal)-dfiles), "", 3); if(level == 1) check_legal(tname, directory, (float)(dsize)/1024, ((*filestotal)-dfiles), strlen(getvalue(tname, "transdir"))); if(debug > 0) printf("process_dir Ende\n"); return dsize; }
|
Wie krieg ich nun das array an sich weg? Oder wird das schon gekillt mit den verschiedenen free(*dir...)'s ?
gruß xodi |