0
Mingw x86_64 v11.2.0
Windows 10 21H2

My current problem is that this function can normally read files and folders in the directory, but if it is saved in the pointer and printed on the screen, a single file will appear repeatedly.

These are the files in the directory.

.
..
main.c
main.exe
README.md
test.txt

The following is the source code I wrote:

#include "../DeleteCompletely.h"
#include <dirent.h>

#define TotalNumber      4096
#define TotalFileNameLen 4096

typedef struct dirent DIRENT;

typedef struct {
    DIR    *dir_ptr;
    DIRENT *dirent_ptr;
    char  **pathSet;
    size_t  number;
} snDIR;

static snDIR *getAllFile(const char *path)
{
    snDIR *dirSet = (snDIR *)malloc(sizeof(snDIR));
    dirSet->dir_ptr = opendir(path);
    size_t loopIndex;

    dirSet->pathSet = (char **)malloc(sizeof(char **) * TotalNumber);
    if(dirSet->dir_ptr) {
        dirSet->number = 0;
        loopIndex = 0;
        while ((dirSet->dirent_ptr = readdir(dirSet->dir_ptr)) != NULL) {
            dirSet->pathSet[loopIndex] = (char *)malloc(TotalFileNameLen);
            dirSet->pathSet[loopIndex] = dirSet->dirent_ptr->d_name;

            dirSet->number++;
            loopIndex++;
        }
        closedir(dirSet->dir_ptr);
    }

    return dirSet;
}

int main(int argc, char **argv)
{
    snDIR *set = getAllFile(".");

    for(size_t x = 0; x < set->number; ++x) {
        printf("%s\n", set->pathSet[x]);
    }

    free(set);
    return 0;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
  • DeleteCompletely.h #include #include #include #include #include #include #include –  Sep 27 '22 at 05:30
  • 1
    Please don't tag multiple irrelevant languages. And please **[edit]** your question if you need to improve it. – Some programmer dude Sep 27 '22 at 05:32
  • Also please take some time to refresh [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Sep 27 '22 at 05:33
  • And in C you [should not cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858). Also build with extra warnings enabled, and treat them as errors (like for example `-Wall -Wextra -Werror`). – Some programmer dude Sep 27 '22 at 05:34
  • You should add the additional necessary includes to the code shown. These two lines are just a giant memory leak factory: `dirSet->pathSet[loopIndex] = (char *)malloc(TotalFileNameLen); dirSet->pathSet[loopIndex] = dirSet->dirent_ptr->d_name;` You'd get the same result without the leak if you just removed the first line. If you want to allocate space for the name and copy it use `strcpy`. Personally I'd skip the overly large allocation and use `strdup`. – Retired Ninja Sep 27 '22 at 05:35

1 Answers1

0

The problem are these assignments:

dirSet->pathSet[loopIndex] = (char *)malloc(TotalFileNameLen);
dirSet->pathSet[loopIndex] = dirSet->dirent_ptr->d_name;

The first make dirSet->pathSet[loopIndex] point to one location. The second make it point somewhere completely different.

Instead of the second assignment you need to copy the string:

strcpy(dirSet->pathSet[loopIndex], dirSet->dirent_ptr->d_name);

You're also wasting quite a lot of memory by always allocating a large amount or memory. The likelihood of the string being that large are rather small. Instead use the string length as the base size:

dirSet->pathSet[loopIndex] = malloc(strlen(dirSet->dirent_ptr->d_name) + 1);
// +1 for the null terminator
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I understand. Thank you for your answer. Sometimes such low-level mistakes are not easy to notice. –  Sep 27 '22 at 05:39
  • Yes, there is too much memory allocated in my code. I will modify it. –  Sep 27 '22 at 05:44