Segmentation Fault before main

I’ve been running into a problem where somehow my code is causing segmentation faults before any of my main actually runs.

Usually, this means that the data structures that your main tries to place in the automatic storage area overflow the stack. In your situation, it looks like the GRAPH is a suitable suspect to do just that: it has a 2D array with 571536 pointers, which could very well overflow the stack before your main gets a chance to start.

One solution to this problem would be moving the GRAPH into the static area: since you allocate it in the main, it’s going to be only one instance of it anyway, so declaring it static should fix the problem:

static GRAPH g;

You might also want to allocate it in the dynamic area using malloc, but in this case it probably does not matter.

Leave a Comment