This is part of my program. parameters.path is a string that contains a path to the file I will be working with, but that is not in this code.
typedef struct directory {
char *name;
char *path;
} directory;
void insertDir(directory*** array , char * path, char* name, int* length) {
directory *p = malloc(sizeof(directory));
p->path = malloc(strlen(path)+ 1);
strcpy(p->path, path);
p->name = malloc(strlen(name)+ 1);
strcpy(p->name, name);
*array = (directory**) realloc( *array , (*length) * (sizeof(directory*)));
*array[(*length)] = p;
(*length)++;
}
int main(int argc , char** argv) {
directory** array = NULL;
int lenght = 0;
while(true) {
insertDir(&array, parameters.path, name , &lenght);
}
return 0;
}
It fails on the third realloc with segmentation fault. Can you help me please?