Reputation: 98
I'm aiming to implement an algorithm to check if a graph is topologic. But well, that's not the issue here. I'm getting a core dump when trying to initialize my graph structure with the given data.
Program received signal SIGSEGV, Segmentation fault.
0x00000000004009a3 in initGraph (numberNode=15, numberArc=17, g=0x1) at src/topologicGraphOp.c:45
45 g->numberNode = numberNode;
I'm really confused because the issue seems so obvious and yet, I cannot find it after various tries so I'm hoping that someone will find that dumb error like "bah, it's obvious" because well... I'm out of luck...
Here is the structure :
typedef struct {
int numberNode;
int numberArc;
int *distances;
int *predecessors;
float **matrix;
queue *successors;
char *graphTitle;
char **nodeDescription;
int *time;
} graph;
And the function where the core dump seem to appear:
void initGraph(int numberNode, int numberArc, graph *g) {
int i, j;
g->numberNode = numberNode;
g->numberArc = numberArc;
g->nodeDescription = malloc(numberNode * sizeof (char));
g->matrix = (float **) calloc(g->numberNode, sizeof (float*));
for (i = 0; i < g->numberNode; i++) {
g->matrix[i] = (float *) calloc(g->numberNode, sizeof (float));
g->nodeDescription[i] = malloc(sizeof (char));
}
}
My main function just calls the initializing function. Thanks a lot!
edit: The solution in the comment :) I forgot to malloc my graph pointer before using it in the function.
Upvotes: 2
Views: 1670
Reputation: 6947
A segmentation fault virtually always means you are messing up a pointer in some way. Either during allocation, or during dereference (for reading or writing, that's the same thing here). You might also have changed the address it points to, for example by forgetting to dereference it on assignment (similar to *g = ...;
becomes g = ...;
); that particular mistake would be less likely with structures, but you never know...
Consequently, when you get a segmentation fault, always double-check all pointers involved in the problematic statement. In this case, g
is the only pointer involved (both the parameter and the field numberNode
are non-pointer variables), so g
is the suspect part.
You don't show us how you call the initialization function, but in this case the code is simple enough that the cause should be obvious once you look at where the g
parameter to initGraph()
is coming from, and what its value (and address) is on entry to initGraph()
. Is g
allocated at all? Does g
point to a sufficiently large block of memory to hold the entire structure? Has any relevant field (e.g. in the case of a pointer to a structure that itself contains pointers) been initialized, and is the value reasonable immediately before the problematic statement?
If you are still uncertain, look at the address and value of that pointer, either through the debugger or by adding something like printf("&g=(%p)",g);
to the top of initGraph()
. Invalid pointers have a tendency to stand out quite well from the noise of actual memory addresses.
Upvotes: 1
Reputation: 8220
Calling from main()
must be like this:
graph g;
initGraph(1, 1, &g);
or like this:
graph *g = malloc( sizeof(graph) );
initGraph(1, 1, g);
EDIT:
In the comments there is a multivoted question about how I explained that there is an error in the code that is not shown in the OP query.
Lets see beginning of the function:
void initGraph(int numberNode, int numberArc, graph *g) {
int i, j;
g->numberNode = numberNode;
}
And the error:
Program received signal SIGSEGV, Segmentation fault. 0x00000000004009a3 in initGraph (numberNode=15, numberArc=17, g=0x1) at src/topologicGraphOp.c:45 45 g->numberNode = numberNode;
The error mention line 45
and I was asked question in comment about how we can know where the line 45
. However, I immediately removed question because near the line number error show line content, so we can find that line in mentioned code.
Code at line 45
is enough simple and may cause error only when pointer g
is referred to wrong memory.
The pointer g
is not modified by this function before use. This function supposes the valid memory pointed by function param g
.
Therefore, function is called with wrong pointer for param g. So I just suggested valid and typical approach to proper initialize variable and call such function with such param.
Upvotes: 2