0

I have to use an undirected weighted graph (adjacency matrix) for this program.

typedef struct graph {
  int n;      /* Number of vertices */
  Boolean *visited; /* Will be later used */
  double **mat;  /* It has to be a double */
} Graph;

Function used to initialize the graph:

void InitializeGraph(Graph* g, int max)  {
    int i;

    g->visited = malloc(max * sizeof(Boolean));

    g->mat = calloc(max, sizeof(double*));
    for (i = 0; i < max; i++)
        g->mat = calloc(max, sizeof(double));
    g->n = max;
}

Right now I'm just trying to add an edge to my graph, but for some odd reason it is not working and I don't know why. Here is the function I am trying to use for that:

void Add(Graph *g, int v1, int v2, double weight){
    g->mat[v1][v2] = weight;
    g->mat[v2][v1] = weight;
}

And here is how my program reads the input and how it calls the function Add(). The reading seems to be working fine from what I've tested.

Graph g;
int i, j, aux;
double daux;

scanf("%d%*c", &aux); /* This is the number of vertices */
InitializeGraph(&g, aux);

for(i = 0; i < g.n; i++){
    for(j = 0; j < g.n; j++){
        scanf("%lf%*c", &daux);
        Add(&g, i, j, daux);
    }
}

Running the program on gdb, here is what shows up:

(gdb) run

Starting program: /home/mintroot/Desktop/media

5

0 5 3 2 2

Program received signal SIGSEGV, Segmentation fault.

0x00000000004007a4 in Add ()

Using this command line to compile the program:

gcc -std=c99 -Wall -pedantic -Werror -o media program.c -lm

What am I doing wrong and how can I fix this?

5
  • What is g.quant? I don't see it in the type definition Commented May 28, 2015 at 17:19
  • My bad, I fixed it. It was supposed to be g.n ...I changed the name because the program was all in portuguese Commented May 28, 2015 at 17:20
  • No worries, I think your issue is the nested for loops. mat is only allocated max number of double* but in the "last" iteration of the loop you are way past that trying to access mat[max][max] which is invalid memory. (although you probably access invalid memory before this case, it is the easiest one to see) You might want to try printing i and j and seeing how far it goes. Commented May 28, 2015 at 17:25
  • You probably just want to change g->mat = calloc(max, sizeof(double*)); to g->mat = calloc(max*max, sizeof(double*)); in your InitializeGraph function Commented May 28, 2015 at 17:31
  • I just tried printing i and j, and it was working normally. Neither of them got higher than g.n ... I also tried changing to max*max and it didn't work either... I have absolutely no idea what could be causing the problem :\ Commented May 28, 2015 at 17:37

1 Answer 1

1

Your initialization function is initializing the matrix incorrectly:

g->mat = calloc(max, sizeof(double*));
for (i = 0; i < max; i++)
    g->mat = calloc(max, sizeof(double));

Note that each iteration of the loop is recording the resulting pointer in the same (wrong) place: g->mat. The quickest way to fix it would be this:

g->mat = calloc(max, sizeof(double*));
for (i = 0; i < max; i++)
    g->mat[i] = calloc(max, sizeof(double));

For what it's worth, do be aware that your mat is not a 2D array, but rather an array of pointers to 1D arrays. You can access elements via the same syntax you would use with a bona fide 2D array, though, so it may not actually matter to you.

Props to @Dave, by the way, who earlier posted and then deleted the same code correction.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, it worked. Can't believe I missed that! Also, thank you for the heads-up for the array of pointers, I will also be looking to correct that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.