gpt4 book ai didi

C malloc 段错误

转载 作者:太空宇宙 更新时间:2023-11-04 04:25:52 25 4
gpt4 key购买 nike

在函数 RenameSubs() 中,当我尝试调试时,我在这行代码处遇到段错误

subsdir[i] = (char*)malloc((GetStringSize(moviesdir[i]) + 1 + 4) * sizeof(char));

我不明白为什么 - 你能解释一下吗?

int main()
{
int number;
char *mainDirectory = NULL,**names = NULL;

printf("Give the movies directory: ");
mainDirectory = ReadMainDirectory();

if(GetFiles(&mainDirectory,&names,&number) != 0)
{
RenameSubs(number,mainDirectory,names);
system("PAUSE");
return 1;
}

system("PAUSE");
return 0;
}

char* ReadMainDirectory()
{
char *dir,c;
int size = 1;

dir = (char*)malloc(size+1);
dir[size-1] = '\0';

while((c = getchar()) != '\n')
{
dir[size-1] = c;
size++;

dir = (char*)realloc(dir,size+1);
dir[size-1] = '\0';
}

return dir;
}

int GetFiles(char **dir,char ***names,int *number)
{
struct dirent *dp;
DIR *fd;

if ((fd = opendir(*dir)) == NULL)
{
printf("Can't open directory %s!\n",*dir);
return 0;
}

*number = 0;

*names = (char**)malloc(((*number)+1) * sizeof(char*));

while ((dp = readdir(fd)) != NULL)
{
if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, ".."))
{
continue;
}

(*names)[*number] = strdup(dp->d_name);

(*number)++;

*names = (char**)realloc(*names,((*number)+1) * sizeof(char*));
}

closedir(fd);

return 1;
}

int GetStringSize(char *string)
{
int size = 0;

while(string[size] != '\0')
{
size++;
}

return size;
}

int StringEndsWith(char* string,char* extension)
{
int size;
char *strextension = NULL;

size = GetStringSize(string);

strextension = (char*)malloc(5 * sizeof(char));

strextension[0] = string[size-4];
strextension[1] = string[size-3];
strextension[2] = string[size-2];
strextension[3] = string[size-1];
strextension[4] = '\0';

if(strcmp(strextension,extension) == 0)
{
return 1;
}

return 0;
}

void RenameSubs(int number,char* mainDir,char** filenames)
{
int i,mainDirSize,movieDirSize[number],moviesdirnumber,subsdirnumber,j,y;
char **moviesdir = NULL,**subsdir = NULL,**subsdest = NULL,**moviesdirfilenames = NULL,**subsdirfilenames = NULL,*moviesname = NULL;

moviesdir = (char**)malloc(number * sizeof(char*));

mainDirSize = GetStringSize(mainDir);

for(i=0;i<number;i++)
{
movieDirSize[i] = mainDirSize + 1 + GetStringSize(filenames[i]);

moviesdir[i] = (char*)malloc((movieDirSize[i]+1) * sizeof(char));

strcpy(moviesdir[i],mainDir);
strcat(moviesdir[i],"\\");
strcat(moviesdir[i],filenames[i]);

GetFiles(&moviesdir[i],&moviesdirfilenames,&moviesdirnumber);

subsdir[i] = (char*)malloc((GetStringSize(moviesdir[i]) + 1 + 4) * sizeof(char));

strcpy(subsdir[i],moviesdir[i]);
strcat(subsdir[i],"\\");
strcat(subsdir[i],"Subs");

GetFiles(&subsdir[i],&subsdirfilenames,&subsdirnumber);

subsdest[i] = (char*)malloc((GetStringSize(subsdir[i]) + GetStringSize(subsdirfilenames[0]) + 1) * sizeof(char));

strcpy(subsdest[i],subsdir[i]);
strcat(subsdest[i],"\\");
strcat(subsdest[i],subsdirfilenames[0]);

for(j=0;j<moviesdirnumber;j++)
{
if(StringEndsWith(moviesdirfilenames[j],".mkv") || StringEndsWith(moviesdirfilenames[j],".mp4") || StringEndsWith(moviesdirfilenames[j],".avi"))
{
moviesname = (char*)malloc((GetStringSize(moviesdirfilenames[j]) - 4 + 1) * sizeof(char));

for(y=0;y<(GetStringSize(moviesdirfilenames[j]) - 4);y++)
{
moviesname[y] = moviesdirfilenames[j][y];
}

moviesname[y] = '\0';

break;
}
}
}
}

最佳答案

您正在使用指针数组,但没有先对其进行初始化。初始化它。

在循环中

 subsdir[i] = malloc((GetStringSize(moviesdir[i]) + 1 + 4) * sizeof(char));

这是第二步。首先你需要初始化

 subsdir = malloc (sizeof(char*) * number);

关于C malloc 段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41647521/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com