闭合会导致munmap_chunk():无效的指针?

发布于 2025-02-02 02:57:40 字数 1494 浏览 3 评论 0原文

因此,我正在进行一种练习,以便prevedes打开目录并保存他的文件以将其用于其他功能。代码是:

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;
}

其他信息,pathsave定义为“/salvataggi”。在WSL中,它有效。

现在,当我进行练习时,终端给了我一个Munmap块错误。我已经完成了一些文本,但是只能删除loteir()我可以消除错误。现在,我不想在程序结束后打开目录,我想知道为什么应该是封闭式导致指针错误的封闭功能。

另外,我使用Visual Studio代码。我有窗口,所以我将WSL用于系统调用。

PS:对不起,我的英语

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文