2

I wrote a program in C langage. I have no problems when trying to run it with my small txt file as an argument. Unfortunately when i am trying to load much bigger file, i am getting Segmentation fault (core dumped. Even 1 line of my main function is not executed. Here is part of code responisble for starting and loading txt file as my argv[1] argument.I dont really see where is the problem.Big Txt files are about 13 MB. I am working linux(ubuntu). I would be grateful for help.

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

typedef struct 
{
    int x;
    int y;
    int wage;   
}Edge;


int main(int argc, char *argv[]) {

printf("Program starts");


int x,y,z;
int counter = 1;
int N,E;   //node,edges

FILE *fid;   
fid = fopen(argv[1],"r");

fscanf(fid,"%d%d",&N,&E);   

Edge Graph[E];
int visited[N+1];


    while(counter <= E){
        fscanf(fid, "%d%d%d", &x, &y,&z);
        Graph[counter-1].x=x;
        Graph[counter-1].y=y;
        Graph[counter-1].wage=z;
        counter=counter+1;
    }   


printf("\nWe load all edges. Here is our Graph");
fclose(fid) ;  


printf("Program ends");
return 0;


}
Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
user3402584
  • 410
  • 4
  • 8
  • 18
  • 2
    If `Graph` gets big, try to use dynamic memory allocation : `Edge* Graph=malloc(E*sizeof(Edge));if(Graph==NULL){printf("malloc failed E=%d\n",E);exit(1)}` ...`free(Graph);` Do the same for `visited`. Check if file was correctly opened `if(fid==NULL){printf("unable to open file %s\n",argv[1]);exit(1);}` – francis Mar 15 '15 at 14:11
  • To spot where the problem failed, add `fflush(stdout);` after `printf()` statements. `sdtout` is buffered. Or use a debugger such as `gdb`. Take a look at http://stackoverflow.com/questions/8969665/how-do-i-find-segmentation-fault-from-multiple-files-using-gdb – francis Mar 15 '15 at 14:24

1 Answers1

2

First some approximations:

  • With 13MB of data, your text file contains certainly over 1 million of edges (assuming that node x and y are each representend in average with 3 digits followed by a space, wage in average 1 digit followed by a space) and at least 1400 nodes.

  • Your variable length arrays Graph[E] and visited[N+1] are local variables, hence stored on the stack. Assuming 4 bytes per integer, that's more than 12 MB of data to put on the stack.

The amount of data that you require on the stack exceeds the usal default stack size on most linux system (8 MB).

You could consider increasing the stack size as explained in this SO question.

But you should really better consider dynamic allocaton:

Edge *Graph =  calloc (sizeof(Edge),E);
int *visited = calloc (sizeof(int), N+1);
if (Graph==NULL || visited==NULL) {
     printf ("Oops ! Out of memory (%d edges, %d notes) !!", E, N+1); 
     exit (1);   // or some other kind of error handling 
}
Community
  • 1
  • 1
Christophe
  • 68,716
  • 7
  • 72
  • 138