I had initialized a 2D array using malloc for adjacency matrix of a large graph and then initializing each index with 0 or 1 depending upon the edge list.But I m getting a segmentation fault. Here is my code.
#include <stdio.h>
#include <stdlib.h>
int MAX = 50000;
void clustering(int **adj);
int main()
{
int i, j, k;
FILE *ptr_file1;
int **adj;
adj = (int **)malloc(sizeof(int *)*MAX);
for(i=0;i<MAX;++i)
adj[i] = (int *)malloc(sizeof(int)*MAX);
struct adjacency
{
int node1;
int node2;
};
struct adjacency a;
ptr_file1 = fopen("Email-Enron.txt","r"); //Opening file containing edgelist of approx 37000 nodes.
if (!ptr_file1)
return 1;
while(fscanf(ptr_file1,"%d %d",&a.node1, &a.node2)!=EOF)
{
adj[a.node1][a.node2] = 1; //Getting segmentation fault here
adj[a.node2][a.node1] = 1;
printf("adj[%d][%d] = %d adj[%d][%d] = %d\n",a.node1,a.node2,adj[a.node1][a.node2],a.node2,a.node1,adj[a.node2][a.node1]);
}
clustering(adj);
return (0);
}
Here is my output
......
......
adj[85][119] = 1 adj[119][85] = 1
adj[85][154] = 1 adj[154][85] = 1
adj[85][200] = 1 adj[200][85] = 1
adj[85][528] = 1 adj[528][85] = 1
adj[85][604] = 1 adj[604][85] = 1
adj[85][661] = 1 adj[661][85] = 1
adj[85][662] = 1 adj[662][85] = 1
adj[85][686] = 1 adj[686][85] = 1
adj[85][727] = 1 adj[727][85] = 1
adj[85][1486] = 1 adj[1486][85] = 1
adj[85][1615] = 1 adj[1615][85] = 1
adj[85][2148] = 1 adj[2148][85] = 1
adj[85][2184] = 1 adj[2184][85] = 1
adj[85][2189] = 1 adj[2189][85] = 1
adj[85][2190] = 1 adj[2190][85] = 1
adj[85][2211] = 1 adj[2211][85] = 1
adj[85][3215] = 1 adj[3215][85] = 1
adj[85][4583] = 1 adj[4583][85] = 1
adj[85][4585] = 1 adj[4585][85] = 1
adj[85][4586] = 1 adj[4586][85] = 1
adj[85][4589] = 1 adj[4589][85] = 1
adj[85][4590] = 1 adj[4590][85] = 1
Segmentation fault (core dumped)
What is wrong here.Pls help...
newinstead ofmalloc()in C++, but not one line of this code is actually C++, it's C. :)malloc. Compile with all warnings and debug info (gcc -Wall -g). Use the debugger (gdb). Use valgrind