D
David d'Angers
hi group:
i've been struggling with this simple problem with dismay
i need all the file names in a directory
since the number of files in a directory is unknown, a friend here in
the group hinted me to use dynamic memory allocation: namely functions
like malloc etc
in the spirit of code re-use
i tried to find similar codes in the program "tree"
and found two wrapper functions xmalloc, xrealloc
without knowing why, i assume these wrappers are easier and safer to
use than the originals
so i wrote the following function read_image_files(), using those two
wrappers
i assume there are only 50 files in a directory,so
#define MAX_FILE_NUM 50
but when there are more than 50, my program eats up the CPU
and i don't know why
especially:
char **imagefiles = (char **)xmalloc(sizeof(char *) * MAX_FILE_NUM);
char **imagefiles = (char **)xmalloc(sizeof(char **) * MAX_FILE_NUM);
i have no idea which one to use and what the difference is
char ** read_image_files (char *dir_name)
{
char **imagefiles = (char **)xmalloc(sizeof(char *) *
MAX_FILE_NUM);
DIR *d;
struct dirent *ent;
int count = 0;
/*navigate the directory*/
while ((ent = (struct dirent *) readdir (d)))
{
count++;
/* if there's more files than predicted, add more*/
if(count > MAX_FILE_NUM)
{
imagefiles = (char
**)xrealloc(imagefiles,sizeof(char *) * (sizeof(imagefiles)+ 1));
}
imagefiles[count-1] = ent->d_name;
}
return imagefiles;
}
void *xrealloc (void *ptr, size_t size)
{
register void *value = realloc (ptr,size);
if (value == 0) {
fprintf(stderr,"tree: virtual memory exhausted.\n");
exit(1);
}
return value;
}
void *xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0) {
fprintf(stderr,"tree: virtual memory exhausted.\n");
exit(1);
}
return value;
}
i've been struggling with this simple problem with dismay
i need all the file names in a directory
since the number of files in a directory is unknown, a friend here in
the group hinted me to use dynamic memory allocation: namely functions
like malloc etc
in the spirit of code re-use
i tried to find similar codes in the program "tree"
and found two wrapper functions xmalloc, xrealloc
without knowing why, i assume these wrappers are easier and safer to
use than the originals
so i wrote the following function read_image_files(), using those two
wrappers
i assume there are only 50 files in a directory,so
#define MAX_FILE_NUM 50
but when there are more than 50, my program eats up the CPU
and i don't know why
especially:
char **imagefiles = (char **)xmalloc(sizeof(char *) * MAX_FILE_NUM);
char **imagefiles = (char **)xmalloc(sizeof(char **) * MAX_FILE_NUM);
i have no idea which one to use and what the difference is
char ** read_image_files (char *dir_name)
{
char **imagefiles = (char **)xmalloc(sizeof(char *) *
MAX_FILE_NUM);
DIR *d;
struct dirent *ent;
int count = 0;
/*navigate the directory*/
while ((ent = (struct dirent *) readdir (d)))
{
count++;
/* if there's more files than predicted, add more*/
if(count > MAX_FILE_NUM)
{
imagefiles = (char
**)xrealloc(imagefiles,sizeof(char *) * (sizeof(imagefiles)+ 1));
}
imagefiles[count-1] = ent->d_name;
}
return imagefiles;
}
void *xrealloc (void *ptr, size_t size)
{
register void *value = realloc (ptr,size);
if (value == 0) {
fprintf(stderr,"tree: virtual memory exhausted.\n");
exit(1);
}
return value;
}
void *xmalloc (size_t size)
{
register void *value = malloc (size);
if (value == 0) {
fprintf(stderr,"tree: virtual memory exhausted.\n");
exit(1);
}
return value;
}