The following code gets segmentation fault:
void dfs_visit(graph g, int u)
{
// printf("u: %d\n",u);
/* g.vertices[u].color = GRAY;
g.vertices[u].d = time++;
printf("before visit for loop\n");
for (int i = 0;i < g.number_vertices;i++)
{
if (i != u)
{
if (g.vertices[i].color == WHITE)
{
g.vertices[i].p = u;
// dfs_visit(g, i);
}
}
}
g.vertices[u].color = BLACK;
g.vertices[u].f = time++;
printf("v: %d d:%d f:%d\n",u,g.vertices[u].d,g.vertices[u].f);*/
}
void dfs(graph g)
{
printf("whats wrong with me\n");
for (int i = 0;i < g.number_vertices;i++)
{
g.vertices[i].color = WHITE;
g.vertices[i].p = NIL;
}
time = 0;
printf("in dfs\n");
for (int i = 0;i < g.number_vertices;i++)
{
if (g.vertices[i].color == WHITE)
{
// dfs_visit(g, i);
printf("i: %d\n",i);
}
}
}
With line 37 commented it reaches inside line 38 and prints everything. But if line 37 is uncommented, then it does not even show "in dfs" and throws some segmentation fault. To debug this, I even commented everything inside dfs_visit. So it is basically a do-nothing function. Even if I remove "graph g" from the declaration of the dfs_visit, there is no segmentation fault. This is funny because why would it get segmentation fault in the first place. Secondly as it turns out that the problematic area is line 37 so segmentation fault is supposed to happen at that place. Why would it not reach line 32 and print it?
PS: It seems like segmentation fault has some kind of mind and complex psychology (like it becomes so angry that it shows some tantrum - "I won't even let it print `in bfs`").