Hello I am getting error as segmentation fault when my file contains large number of line(approx. 44000. However if I run the same program with lesser number of lines, it works fine. I am not getting the reason for this error and how to rectify it.

int main() 
        FILE *fp,*file,*f1;
    int **phone;
    int count = 0;
    f1 = fopen("phones.txt","r");  //open files which has phones
    int noOfLines = count_lines();   // get no of lines in phone.txt
    //int phone[30][noOfLines]={0};    // variable-sized object may not be initialized

    phone = (int**) calloc(30,sizeof(int*));  // create array using malloc
    int i;
    for (i=0;i<30;i++)
    phone[i] = (int*) calloc(noOfLines,sizeof(int));

    char line2[128];
    if ( f1 != NULL )              // check if the files exist
    while(fgets(line2, sizeof(line2), f1) != NULL)   // get each line from file

    line2[strlen(line2)-1]='\0';  // to remove the last \n 
    file = fopen("list.txt","r");    // open file which has the list of .phseg -->this is the large file
    if ( file != NULL)
    char line1[128];

    while(fgets(line1, sizeof(line1), file) != NULL) // read one file at a time

    line1[strlen(line1)-1]='\0';  // to remove the last \n 
    fp = fopen(line1, "r"); 

    if ( fp != NULL)
    char line[128];

    while(fgets(line, sizeof(line), fp) != NULL)  // read contents of .phseg linyByLine
    char *words[10];
    int nwords;
    nwords = getwords(line, words, 20);
    char str[3];
    int a1 = atoi(words[0]);      // convert string to integer
    int a2 = atoi(words[1]);
    int a3 = a2 - a1;       // get thr number of frames

    printf("file not found");
    int j;  


Please help me out. I am stuck at this point.

I am not getting the reason for this error and how to rectify it.

A segmentation fault is when you try to access memory outside of your address space. It's nearly always caused by dereferencing an invalid pointer address or using an out of range index for an array. Those two things should be the first you look for.

Since I don't have the rest of your code or your files, you'll have to debug this yourself. Any decent debugger will let you run until a failure; you can at least see which line it occurs on and whatever pointer or index values are set at the time.

Another thing to consider is the 16-bit boundary. Since you guesstimated 44,000, and that's close to 32,767, and Turbo C is distressingly common, you'll probably want to consider issues where 16-bit integers are overflowing somewhere and wreaking havoc. If you're using Turbo C, of course.

This article has been dead for over six months. Start a new discussion instead.