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`").

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

Are you sure this is C...Look at the for loop

for (int i = 0;i < g.number_vertices;i++)

Aha! I got it. See, you're pushing a copy of your entire graph structure on the stack every time you call a function with g. Your definitions need to look more like this.

void dfs_visit( graph *g, int u );
void dfs( graph *g );

And when you call dfs for the first time, do it like this:

dfs( &the_real_graph );

Structs, when passed on the runtime stack, copy their entire contents and create a local copy of themselves. So not only are you torturing the poor runtime stack, you're also going to get absolutely nothing out of using it that way, since any modifications to g will only be localized, and when the function ends, no changes will have taken place. More often than not, you need to pass structures by reference to have anything meaningful happen.

Oh, and remember that when you're dealing with a pointer to a struct, you have to use the '->' member-of operator rather than the '.'.

Edited 6 Years Ago by IsharaComix: use -&gt;, not .

Comments
An entirely plausible explanation :)

Aha! I got it. See, you're pushing a copy of your entire graph structure on the stack every time you call a function with g. Your definitions need to look more like this.

void dfs_visit( graph *g, int u );
void dfs( graph *g );

And when you call dfs for the first time, do it like this:

dfs( &the_real_graph );

Structs, when passed on the runtime stack, copy their entire contents and create a local copy of themselves. So not only are you torturing the poor runtime stack, you're also going to get absolutely nothing out of using it that way, since any modifications to g will only be localized, and when the function ends, no changes will have taken place. More often than not, you need to pass structures by reference to have anything meaningful happen.

Oh, and remember that when you're dealing with a pointer to a struct, you have to use the '->' member-of operator rather than the '.'.

You are an angel. It works now!!!

Segmentation fault is so dumb. May be there should be some JVM like think where we can run the development version which can keep a stacktrace and show it.

This article has been dead for over six months. Start a new discussion instead.