I have an assignment to write a Graph ADT in C, which implements run breadth first search. When I call the Graph destructor I get: segmentation fault (core dumped).
The Graph ADT uses an array of Lists to represent adjacent vertices.
The element number in the array is the same as the numerical label for that vertex.
The Graph ADT has 3 array fields, color, distance, and, parent. In each the ith element of the array is the numerical label of the vertex and the data stored at that index is either a representation of the color (for BFS), the distance from the source vertex used in BFS, or the direct parent of that vertex.
Each of these arrays are initialized as follows:
G->color = calloc(n+1, sizeof(int));
‘n’ is the number a vertices in G.
Here is what I attempted to implement for the destructor:
void freeGraph(GraphRef* pG){
int i;
int n = getOrder(*pG) /* the number of vertices in pG */
for(i = 1; i <=n; i++){
freeList((*pG)->adj[i]); /*free the list containing i's adjacent vertices*/
}
free((*pG)->color);
free((*pG)->distance);
free((*pG)->parent);
*pG = NULL;
}
I am not sure what I am doing wrong. Any advice would be greatly appreciated!
Thank you for your feed back! In my case I am disregarding the 0th element of all arrays, so that their index can represent each vertices integer label (starting at 1).
I changed my code as follows and it appears be working: