c - Dynamic Allocation of 2D Array -
i implementing graphs using adjacency matrix, unable solve segmentation fault. can me in guiding dynamic allocation of 2 dimensional matrix? want know how 2-d array stored in memory , how accessed.
#include<stdio.h> #include<stdlib.h> struct graph{ int v; // represent number vertex... int e; //to represent number edge..... int **adj; // 2 dimensional matrix form adjacency matrix... }; struct graph *adjmatrixofgraph(){ int i; //for scanning edges between them .... int u,v; // loop while initliasing adjacency matrix... struct graph *g=(struct graph*) malloc(sizeof(struct graph)); // if(!g){ printf("memory error"); return; } printf("number of vertices"); scanf("%d",&g->v); printf("%d",g->v); printf("number of edges"); scanf("%d",&g->e); g->adj=(int **)malloc(sizeof(g->v * g->v)); //allocating memory g->adj); /*dynamic memory allocation 2 dimensional arrays */ /* g->adj = malloc(g->v * sizeof(int )); if(g->adj == null) { printf( "out of memory\n"); } for(i = 0; < g->v; i++) { g->adj[i] = malloc(g->v * sizeof(int )); if(g->adj[i] == null) { printf( "out of memory\n"); } } */ if(!g->adj) { printf("memory error"); return; } for(u=0; u < g->v; u++){ for(v=0; v < g->v; v++){ //printf("%d %d",u,v); g->adj[u][v]=0; //initalising complete adjacency matrix zero. } } //enter edges.. , vertices. //we considering graph undirected 1 ... for(i=0;i< g->e;i++) { scanf("reading edges %d %d ",&u,&v); g->adj[u][v]=1; g->adj[u][v]=1; //if graph directed should have considere 1 side... //g->v[u][v]=1; } return g; } main() { struct graph *g1=adjmatrixofgraph(); //struct graph *adjmatrixofgraph(){ printf("successful"); return 0; }
allocating memory int **adj
done in following way:
first allocate memory number of pointers integers have:
adj = malloc(sizeof(int*) * number_of_integers); /* notice pass sizeof */
next allocate memory each integer individually:
for (i = 0; < number_of_integers; i++) adj[i] = malloc(sizeof(int) * g->e);
and of course every malloc
call needs followed free
call, in similar fashion.
notice don't cast result of malloc.
i've made few other changes code:
update scanf
ensure have no problems newlines remaining in buffer:
printf("number of vertices: "); scanf(" %d", &g->v); printf("number of edges: "); scanf(" %d", &g->e);
initialise them (alternatively, lookup calloc, zero-initialisation you):
for(u=0; u < g->v; u++) // each vertice { for(v=0; v < g->e; v++) // each edge { g->adj[u][v] = 0; } }
the part below i'm not sure about, manually set edges one, right? shouldn't use g->v
, not g->e
?
for(i = 0; < g->v; i++) { printf("reading vertice u: "); scanf(" %d",&u); printf("reading edge v: "); scanf(" %d",&v); if (u > g->v || v > g->e) // simple error handling { printf("input bigger size of vertice/edges\n"); exit(1); } g->adj[u][v] = 1; g->adj[u][v] = 1; }
i able print successful
after this. if want make little easier, compile code -g
flag , if you're on linux ulimit -c unlimited
. create coredump file every time segfault.
then see problem is, run gdb your_app core
, inside run backtrace
. cannot stress how important use debugger in these cases.
Comments
Post a Comment