I'm making a program that basically takes as input a path and can output the total size of all files/directories inside it. the program recusivly scans through the directory givin and sums up the size of all files in all subfolders. heres the directory structure I've made
Code:
struct directory {
char *name;
int size;
int nfiles;
int ndirs;
struct directory *parent;
struct directory **dlist;
File **flist;
};
in order to put a new directory in **dlist I use this function, which should increase the size of the **dlist member and add the new directory to the list
Code:
int dir_add_dir(struct directory *d, struct directory *new)
{
int ndirs;
struct directory **temp;
ndirs = d->ndirs;
if ((temp = (struct directory **)realloc(d->dlist, sizeof(struct directory *) * (ndirs + 1))) == NULL)
{
printf("Memory Error\n");
return -1;
}
d->dlist = temp;
d->dlist[ndirs] = new;
d->size += new->size;
d->ndirs++;
return 1;
}
d being the parent and new being the directory to add.
I keep getting seg faults though, and when I run it with valgrind it is complaining about the realloc.
am I going about this wrong...or just using realloc wrong. There is no way to tell how many elements will have to go in the list, so it must be updated dynamically.