我的代码在小文件上运行没有错误。但是我在大输入(数百万行)上遇到了段错误。请看下面的一些代码:
element* unionFind(void *data)
{
element *retVal = NULL;
retVal = (element *)malloc(sizeof(element));
retVal->data = data;
retVal->rank = 0;
retVal->leader = retVal;
return retVal;
}
主要:
...
int main (int argc, char* argv[])
{
char str[2048];
char* v_S = NULL;
struct set *s_v = NULL;
*计算输入的行数
long *v_L = (long *)malloc(sizeof(long)*ct_lines);
element **v = malloc(sizeof(element)*ct_lines) ;
while(fgets(str,sizeof(str),fp) != NULL)
{
v_S = strtok(str,":");
v_L[i] = atol(v_S);
v[i] = unionFind(&v_L[i]);
s_v = add_vertex(v_L[i], v[i]);
i++;
}
fclose(fp);
for (i = 0; i < ct_lines; i++) //Here segfault
{
free(v[i]); v[i] = NULL;
}
free(v); v = NULL;
free(v_L); v_L = NULL;
}