0

So, I was doing an exercise that prevedes to open a directory and save his files to use them for other functions. The code is:

int main()
{
    printf("%sApro la cartella salvataggi%s\n", COL_YELLOW, COL_GRAY);

    int i = 0;
    int initsaves = 10;
    char **saveFiles = (char **)malloc(initsaves * sizeof(char *));
    int dim = initsaves - 1;

    DIR *saveDir;

    errno = 0;

    if ((saveDir = opendir(pathSaves)) == NULL)
    {
        if (errno == ENOENT)
            mkdir(pathSaves, S_IRWXU | S_IRWXG | S_IRWXO);
        else
        {
            printf("%i\n", errno);
            errExit("Error in opening savefile");
        }
    }

    struct dirent *controlSaves = readdir(saveDir);

    do
    {
        if (i == dim - 1)
        {
            dim *= 2;
            saveFiles = (char **)realloc(saveFiles, dim);
        }

        if (strncmp(controlSaves->d_name, ".", 1) != 0)
        {
            saveFiles[i] = controlSaves->d_name;
            printf("%s\n", saveFiles[i]);
            i++;
        }

        controlSaves = readdir(saveDir);
    } while (controlSaves != NULL);

    free(saveFiles);

    if (closedir(saveDir) == -1)
    {
        errExit("Problema nel chiudere la cartella");
    }

    printf("%sFINITO%s\n", COL_RED, COL_GRAY);
    return 0;
}

Other information, pathSave is defined as "/Salvataggi". In WSL it works.

Now, while I was doing the exercise, the terminal gave me a munmap chunk error. I've done some text, but only deleting the closedir() I could eliminate the error. Now, I don't want let open the directory after the end of the program, and I want to know why should be the closedir to cause a pointer error.

Also, I use Visual Studio Code. I have windows, so I use WSL for the system calls.

PS: Sorry for my english

Dacqu91
  • 3
  • 3
  • Where is `pathSaves` defined? – ryyker May 26 '22 at 15:06
  • ...does `pathSaves` contain an actual directory? – ryyker May 26 '22 at 15:13
  • Compare what you are doing with [this example](https://stackoverflow.com/questions/3554120/open-directory-using-c) – ryyker May 26 '22 at 15:15
  • (By the way, I see you are new here. Welcome! May I suggest that when you post, it is a good idea to hang around for a few minutes to respond to questions, comments, etc.) – ryyker May 26 '22 at 15:17
  • This statement is called: `saveFiles[i] = controlSaves->d_name;` before allocating memory to `savesFiles[i]`. You should have seen compile-time warnings and errors with this code. This indicates what you have posted is not the code you actually ran. – ryyker May 26 '22 at 15:22
  • pathsaves is defined in un header file. Contain the directory to analize. The definition is simply "/Salvataggi" – Dacqu91 May 26 '22 at 15:25
  • ```saveFiles[i] = controlSaves->d_name;``` is not called before allocating memory. But it can be called before the realloc, if you mean that. – Dacqu91 May 26 '22 at 15:27
  • Read my previous comment and edit your post please. By the way, `"/Salvataggi"` is a linux style directory, but you said you are using Windows? – ryyker May 26 '22 at 15:27
  • `saveFiles` is allocated memory, but `saveFiles[i]` is not. (I don't see where you do it in your post anyway.) – ryyker May 26 '22 at 15:28
  • No, it's not. Because it works as a pointer to string, like ```char *string = "content";``` – Dacqu91 May 26 '22 at 15:33
  • After `char **saveFiles = (char **)malloc(initsaves * sizeof(char *));` use something like: `if(saveFiles) { for(i=0 ; i – ryyker May 26 '22 at 15:35

0 Answers0