闭合会导致munmap_chunk():无效的指针?
因此,我正在进行一种练习,以便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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论