I'm going through a directory trying to to build a character array of the files within the directory, but I keep gettting a bus error on my realloc call. Any ideas?
DIR *dirh;
struct dirent *dirp;
char** dir_list;
int dir_count=0;
/* assume 'dir' is a valid directory name */
dirh=opendir(dir);
while((dirp=readdir(dirh))!=NULL)
{
dir_count++;
dir_list=(char*)realloc(dir_list, dir_count*sizeof(char*));
dir_list[dir_count-1]=(char*)malloc((strlen(dirp->d_name)+1)*sizeof(char*));
strcpy(dir_list[dir_count-1], dirp->d_name);
}
closedir(dirh);
It keeps 'bus erroring' on the realloc call. How should I appropriately do this?
Thanks,
-bitwise
DIR *dirh;
struct dirent *dirp;
char** dir_list;
int dir_count=0;
/* assume 'dir' is a valid directory name */
dirh=opendir(dir);
while((dirp=readdir(dirh))!=NULL)
{
dir_count++;
dir_list=(char*)realloc(dir_list, dir_count*sizeof(char*));
dir_list[dir_count-1]=(char*)malloc((strlen(dirp->d_name)+1)*sizeof(char*));
strcpy(dir_list[dir_count-1], dirp->d_name);
}
closedir(dirh);
It keeps 'bus erroring' on the realloc call. How should I appropriately do this?
Thanks,
-bitwise