15,999,229 members
See more:
I create a Dijkstra algorithm program using an adjacency list. My teacher give me this struct.

Where the argument `nom` is vertices, `poids` is weight, `som_a` and `som_b` are vertices, `nbSommets` is number of vertices, `nbArcs` is number of edges, `NB_SOM_MAX` is number of vertices MAX.

I don't have compilation errors and when I execute my program I have a segmentation fault.

What I have tried:

Objective-C
```//list of couples (sommet,poids)
typedef struct maillon{
struct maillon *suiv;
int nom;
int poids;
} MAILLON, *LISTE;

//graph structure
typedef struct graphe{
int nbSommets;
} GRAPHE;

void insere(int som_a, int som_b, int poids, LISTE Adj[]){
LISTE prem=malloc(sizeof(LISTE));
prem->nom = som_b;
prem->poids = poids;
}

//Initialization of the adjacency table: all lists are empty
int i;
for(i = 0; i < G->nbSommets; i++){
}
}

//Load a graph from a file
FILE *f;
int sa,sb,pds,nbArcs;
fscanf(f,"%d",&(G->nbSommets));
fscanf(f,"%d",&nbArcs);
while(nbArcs){
fscanf(f,"%d %d %d",&sa,&sb,&pds);
nbArcs--;
}
fclose(f);
}

//Displaying a graph
void afficheGraphe(GRAPHE G){
int j;
printf("%d\n", G.nbSommets);
for(j = 0; j < G.nbSommets; j++){
}
}
}

//Dijkstra algorithm: displays the shortest path between s and all vertices of G
void dijsktra(int s, GRAPHE G){
int i,j,dist[NB_SOM_MAX], INT_MAX, pred[NB_SOM_MAX], min, nb=0,nbmin=0;
for(i = 0; i<g.nbsommets; i++){="" dist[i]="INT_MAX;" pred[i]="NULL;" }="" dist[0]="0;" s="NULL;" while(f="" !="NULL){" min="G.Adj[0]-">poids;
for(i = 1; i<g.nbsommets; i++){="" if(min=""> G.Adj[i]->poids){
nbmin = i;
}
}
insere(nb, nbmin, min, &S);
nb++;
if(nbmin == 0){
F = F->suiv;
}
else{ // F[nbmin-1]->suiv = F[nbmin+1];
F[nbmin-1] = F[nbmin+1];
}

pred[i] = nbmin;
}
}
}

for(i = 0; i < G.nbSommets; i++){
printf("Chemin optimal de %d à %d : ", i, s);
printf("%d-", i);
j=i;
while(pred[j] != s || pred[j] != NULL){
printf("%d-", pred[j]);
j=pred[j];
}
printf("\n");
}
}

int main(void){
GRAPHE G;
litGraphe("./graphe.txt", &G);
afficheGraphe(G);
dijsktra(0, G);
return 0;
}
```
Posted
Updated 29-Nov-16 22:29pm

## Solution 2

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.

[Français]
1) Ton code n'est pas autonome, donc personne (ici) ne peut le compiler pour voir ce qui ce passe.

2)
Quote:
I have a segmentation fault.
Ca veux dire que ton programme essaie d'écrire à un endroit qui ne lui appartient pas. Soit tu écris après la fin d'un tableau, soit tu utilise un pointeur qui n'est pas initialisé.

3) Utilise le debugger, en exécutant ton programme pas à pas, tu pourra localiser l'endroit ou ça plante. Avec le debugger, tu pourras inspecter les variables pendant l'éxécution.

## Solution 1

It is often difficult to find the sources by just reading the code (I tried it). It may be even sourced by loading invalid values from the file. This looks suspicious because you are reading the index from the file (first arc line value). When this out of range or there are not lines for all indexes, your code will crash later (with missing indexes there will be NULL entries in your struct).

So I suggest to add checks to verify that parameters and variables are valid.

A quick first approach might be using assert - C++ Reference[^].

```#include <assert.h>

void insere(int som_a, int som_b, int poids, LISTE Adj[]){
assert(som_a >= 0 && som_a < NB_SOM_MAX);
/* EDIT: Must be off course Adj
assert(LISTE != 0);
*/

LISTE prem=malloc(sizeof(LISTE));
prem->nom = som_b;
prem->poids = poids;
}

assert(G);
assert(G->nbSommets < NB_SOM_MAX);
int i;
for(i = 0; i < G->nbSommets; i++){
}
}```

For `initAdjGraphe()` it would be even better to set all items to `NULL`.

A final implementation would return an error code and/or print an error message:
```int insere(int som_a, int som_b, int poids, LISTE Adj[]){
if (som_a < 0 || som_a >= NB_SOM_MAX)
{
printf("som_a value %d is invalid\n", som_a);
return -1;
}
/* EDIT: Again it  must be Adj
if (LISTE == 0)
*/
{
return -1;
}
/* ... */
return 0;
}```

v2