Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

realloc problem 1

Status
Not open for further replies.

bitwise

Programmer
Mar 15, 2001
269
US
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
 
First of all, you shouldn't realloc() on every iteration. realloc() is a pretty expensive operation and should only be done periodically. A good way is to create a "chunk size" variable and a variable that keeps track of the current size of the list and then grow the list as needed.

#define CHUNK_SIZE 100

/* ... */

char **dir_list=malloc(CHUNK_SIZE * sizeof *dir_list);
int list_size=CHUNK_SIZE;

/* ... */

/* In your loop */

dir_count++;

if (dir_count > list_size) {
/* Double the size of the list */
list_size*=2;
char **tmp=realloc(dir_list,list_size * sizeof *tmp);
if (tmp!=NULL) {
dir_list=tmp;
}
}

>DIR *dirh;
>struct dirent *dirp;
>char** dir_list;
>int dir_count=0;

>/* assume 'dir' is a valid directory name */
>dirh=opendir(dir);

You should check to make sure opendir() succeeded:

if (dirh!=NULL) {

>while((dirp=readdir(dirh))!=NULL)
>{
> dir_count++;
> dir_list=(char*)realloc(dir_list, dir_count*sizeof(char*));

You should use a temporary pointer here:

Above:

char **tmp;

tmp=realloc(dir_list,dir_count * sizeof *tmp);
if (tmp!=NULL) {
dir_list=tmp;
}

The cast is also unnecessary and buys you nothing. In fact, if you were to cast, you would cast to (char **), since that matches the type of dir_list.

> dir_list[dir_count-1]=(char*)malloc((strlen(dirp->d_name)+1)*sizeof(char*));

A little less messy and better for code maintenance:

dir_list[dir_count-1]=malloc(strlen(dirp->d_name)+1);

Note that you don't want to multiply times sizeof(char *) here because you're allocating space for strlen(dirp->d_name) + 1 elements of type char. Furthermore, sizeof(char) is guaranteed to be 1, so no need to multiply by anything.

> strcpy(dir_list[dir_count-1], dirp->d_name);
>}

>closedir(dirh);

I'm not sure what's causing your bus error, though your code has problems, I can see nothing that would cause this. Try implementing the fixes suggested above and see if it continues or not.

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top