This is a function that i’ve written:
uint32_t file_list(char *path, char ***ls){
DIR *dp;
//uint32_t i;
struct stat fileStat;
struct dirent *ep = NULL;
uint32_t len, count = 0;
int file = 0;
*ls = NULL;
dp = opendir (path);
if(dp == NULL){
fprintf(stderr, "no dir: %s\n", path);
exit(1);
}
ep = readdir(dp);
while(NULL != ep){
count++;
ep = readdir(dp);
}
rewinddir(dp);
*ls = calloc(count, sizeof(char *));
count = 0;
ep = readdir(dp);
while(ep != NULL){
if((file = open(ep->d_name, O_RDONLY)) < 0){
perror("apertura file");
exit(1);
}
if(fstat(file, &fileStat) != 0){
perror("filestat");
free(*ls);
close(file);
exit(EXIT_FAILURE);
}
close(file);
if(S_ISDIR(fileStat.st_mode)){
len = strlen(ep->d_name);
(*ls)[count] = malloc(len+5); /* lunghezza stringa + "DIR \n" */
strcpy((*ls)[count], "DIR "); /* copio DIR */
strcat((*ls)[count++], ep->d_name); /* concateno la stringa DIR con il nome della dir */
ep = readdir(dp);
}
else{
(*ls)[count++] = strdup(ep->d_name);
ep = readdir(dp);
}
}
/*for(i=0; i<count; i++){
free((*ls)[count]);
}*/
(void)closedir(dp);
return count;
}
into the main program i have char **files and then the part where i get the count is count = file_list("./", &files);
What is my problem?
Everbody know that the dynamically allocated memory they (the pointers) might refer to, must be freed but if i free the pointers (with the for loop) then into the main program i got an unexpected behaviour during the file list (duplicate file name, no file name, etc).
In fact if i don’t free the pointers all work perfectly.
So my question is: how to free these pointers?
Thanks in advance!
Your problem is that you allocate and free in the same function, which render your function basically useless (if I understand correctly anyway). If you free in the same function, after it returns (i.e.: in the “main program”), you end up accessing memory segments that have been released to the operating system, which is undefined behavior.
You’d need two functions, one for the allocation (the one above), and one to free it once you’re done, per example:
Keep in mind that your free function will need to know the
countto prevent a buffer overrun.There are other problems with your code (e.g.: not checking the return value of
calloc, etc.), but it would be too long to cover them all here (and don’t necessarily relate to your actual question).