C pointers

Asked

Viewed 131 times

2

Well, guys, I’m doing graph theory on C, and I tried to use dynamic allocation to create an array. Also, my code has a function to fill the matrix with 1 in the received indexes. But I have some error in the allocation of the matrix or in its filling.

The code hangs and returns 3221225477

#include<stdlib.h>
#include<stdio.h>

struct digraph //Estrutura do grafo, V e A são contadores, matrix_adj é um ponteiro para um array de ponteiros
{
    int V , A;
    int **matrix_adj;
};

typedef struct digraph* Digraph;
int **matrix_alloc(int r,int c,int val);
Digraph graph_alloc(int vertex);
void insert(Digraph G, int v, int w);
void show(Digraph G);

int main(void)
{
    Digraph G;
    G = graph_alloc(6);
    insert(G, 1,2);
    insert(G, 1,3);
    insert(G, 2,4);
    insert(G, 3,4);
    insert(G, 4,5);
    insert(G, 5,6);
    show(G);
    return 0;

}

int **matrix_alloc(int r,int c,int val)
{
    int i, j;
    int **matrix = (int**)malloc(r*sizeof(int*));// aloca linhas
    for(i=0;i<r;i++)
    {
        matrix[i] = (int*)malloc(c*sizeof(int));//aloca colunas
        for(j=0;j<c;j++)
        {
            matrix[i][j] = val;
        }
    }
    return matrix;
}

Digraph graph_alloc(int vertex)
{
    Digraph G;
    G->V = vertex;
    G->A = 0;
    G->matrix_adj = matrix_alloc(vertex, vertex, 0);
    return G;
}

void insert(Digraph G,int v, int w)
{
    if(G->matrix_adj[v][w] == 0)
    {
        G->matrix_adj[v][w] = 1;
        G->matrix_adj[w][v] = 1;
        G->A++;
    }
}

void show(Digraph G)
{
    int v, w;
    for(v=0; v<G->V; v++)
    {
        for(w=0; w<G->V;w++)
        {
            printf("%d ", G->matrix_adj[v][w]);
        }
        printf("\n");
    }
    printf("\n");
    printf("Vertices: ", G->V);
    printf("Arestas:  ", G->A);
}
  • What is the mistake? Where?

  • The program opens and already stops working, but this large number of returns that I mentioned makes me believe that it is some memory error, in the function Insert or matrix_alloc

  • Show the table or program before that? I tried the code and it looks good

  • In the computer that I am currently, it stops working before showing, in q I have at home, shows the output matrix, but stands still for a few seconds and then the main function returns 3221225477

  • Strange, I’m not having mistakes, the program goes well

1 answer

3


Attention to this type of typedefs:

typedef struct digraph* Digraph;

You should avoid this kind of typedefs with pointers because they end up masking the guys and giving you the wrong idea, consequently leading you to make mistakes you wouldn’t normally make.

Note that in the creation function of graph_alloc:

Digraph graph_alloc(int vertex)
{
    Digraph G;  // <---- aqui
    G->V = vertex;
    G->A = 0;
    G->matrix_adj = matrix_alloc(vertex, vertex, 0);
    return G;
}

Is using Digraph G as if it were a int or a normal guy, but it’s actually a pointer because of typedef that you did initially. It would be like having done:

struct digraph *G;
G->V = vertex;

Which is wrong. The problem is is creating a pointer, says that at the point of the V becomes real vertex, but did not define where G points out. For this reason the pointer will be pointing to a random location in memory.

This represents undefined and potential behavior Segmentation fault, which is the brake you indicated.

It has several solutions. The simplest keeping all that is is to make the allocation with malloc:

Digraph G = malloc(sizeof(struct digraph));

See code working on Ideone

Personally I advise you to undo the typedef pointer and use only one for base type in order to avoid having to always write struct, thus:

typedef struct digraph digraph;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.