igraph-help
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [igraph] segmentation fault


From: Gábor Csárdi
Subject: Re: [igraph] segmentation fault
Date: Thu, 24 Mar 2011 14:09:14 -0400

You allocate a fixed sized 'b' vector, and then have a component that
is larger than the fixed size. This is why you get the segmentation
fault.

Allocate the size of 'b' dynamically, with calloc(), after checking
the size of the largest component. Allocating big vectors in the stack
is not a good idea anyway. Consider using a debugger, like gdb, and
then you seen where exactly the error happens.

Btw. your program could be simplified if you used igraph_clusters(),
that gives you the sizes of the components directly, and then you can
call igraph_vector_max() to get the size of the largest, etc.

Best,
Gabor

On Thu, Mar 24, 2011 at 1:47 PM, Yi Cao <address@hidden> wrote:
> Hello everyone,
>
> The following is my program for calculating the distribution of components
> size of a given graph. It works for small edge list file. However, when I
> tried the bigger files (which are .txt files and about 20M), it failed. The
> input edge list file is a file in which there are two columns numbers, each
> row contains two numbers representing the two nodes of an edge. (probably
> because the input file is too big, it returned error info when I tired to
> attach it and send it.).
>
> I compiled and ran my program under suse linux:
>
> The program is:
>
>  // compile:
> // gcc componenthistogram.c -I ~/include/igraph -ligraph -L ~/lib -o
> componenthistogram
> // LD_LIBRARY_PATH=/
> //export LD_LIBRARY_PATH
> // ./componenthistogram inputfile
>
> #include <igraph.h>
> #include <stdlib.h>
>
> void free_complist(igraph_vector_
> ptr_t *complist)
> {
>   long int i;
>   for (i=0; i<igraph_vector_ptr_size(complist); i++) {
>     igraph_destroy(VECTOR(*complist)[i]);
>     igraph_free(VECTOR(*complist)[i]);
>   }
> }
>
>
> int main(int argc, char *argv[ ])
>  {
>   igraph_t  g;
>   igraph_vector_ptr_t complist;
>   long int b[1000000]={0};
>   long int i,j;
>
>
> if (argc!=2)
>  {printf("You must suppy edge file name!\n"); exit(0);}
>   //first,read an edgelist in order to reate a graph.
>
>    FILE *input;
>       input=fopen(argv[1], "r");
>        if (!input) {
>           printf("error");
>         }
>
>       igraph_read_graph_edgelist(&g, input,  0, IGRAPH_UNDIRECTED);
>       fclose(input);
>
>    printf("Vertices count: %d\n", (int)igraph_vcount(&g));
>    printf("Edge count : %d\n", (int)igraph_ecount(&g));
>    igraph_vector_ptr_init(&complist, 0);
>    igraph_decompose(&g, &complist, IGRAPH_WEAK, -1,1);
>
>    printf("Components and their sizes:\n");
>    for (i=0; i<igraph_vector_ptr_size(&complist); i++)
>     {
>      printf("%d   ", i);
>      printf("%d\n", (int)igraph_vcount(VECTOR(complist)[i]));
>      }
>
> //now, let us compute maximum compoonent size (to be placed in variable
> max);
> long int max=0;
> long int size;
>
> for (i=0; i<igraph_vector_ptr_size(&complist); i++)
>  {
>    size=(int)igraph_vcount(VECTOR(complist)[i]);
>    if (size>max) max=size;
>  }
>
>
> //compute histogram
> for (i=0; i<igraph_vector_ptr_size(&complist); i++)
>  {
>    size=(int)igraph_vcount(VECTOR(complist)[i]);
>    b[size]=b[size]+1;
>  }
>
> //print the histogram
>  printf("Histogram (first column=size, second column=nr of components with
> this size)\n");
>   for(j=0; j<=max;j++)
>     {
>     printf("%d   %d\r\n",j,b[j] );
>         }
>
> //
>    free_complist(&complist);
> //   igraph_destroy(&g);
> return 0;
>
>  }
>
>  I was thinking, was the segmentation fault a problem of my program or a
> problem of the computer's computation capacity, or a problem of the
> functions (such as igraph_decompse ) in igraph package I used?  Anyone have
> some ideas or solutions about this?  I appreciate your help. Thanks a lot.
>
> _______________________________________________
> igraph-help mailing list
> address@hidden
> http://lists.nongnu.org/mailman/listinfo/igraph-help
>
>



-- 
Gabor Csardi <address@hidden>     MTA KFKI RMKI



reply via email to

[Prev in Thread] Current Thread [Next in Thread]