john johnson
john johnson

Reputation: 21

Segmentation fault in file parsing code

I am getting a segmentation fault when I try to run my program that does matrix addition. I am trying to run the program separately ~1000 times (while timing each run and writing the result to a file).

The problem is, I get segmentation fault after a number of runs - how far I get depends on the size of the matrix. For example, if I run a 10x10 matrix (each instance has randomly generated numbers), I get segmentation fault after exactly 1013 runs. For a 100x100 matrix, I get a segfault at 260 runs.

A quick run through of how the program works is as follows:

  1. Numbers are randomly generated and written to a file depending on the entered input (10x10, 100x100)
  2. The numbers are read in from the file and send to CUDA*
  3. CUDA calculates the results and writes it to a results file (and also times how long the calculation took and writes it to another file)

*This step appears to be causing the segmentation fault according to the GDB debugger. Below is the error output from the debugger and the function that is causing the error.

>Program terminated with signal 11, Segmentation fault.
#0  0x0000000000402f4c in readFromFile(int, char, int&, int&, float*) ()

Here is the actual function:

  void readFromFile(int fd, char byte, int &matrixWidth, int &matrixHeight,float *matrix)
    {
    int tokenIndex = 0;
    char *token = (char*) malloc(500); 
    int matrixIndex = 0;
    while(read(fd,&byte,1)){
        if(isdigit(byte)||byte=='.'){
            token[tokenIndex]=byte;
            tokenIndex++;
        }
        else if(byte==' ' && matrixHeight==0){
            matrixWidth++;
            token[tokenIndex]='\0';
            matrix[matrixIndex]=atof(token);
            //printf("Stored: %d\n",matrixOne[matrixIndex]);
            tokenIndex=0;
            matrixIndex++;
        }
        else if(byte=='\n'){
            matrixHeight++;
            if(tokenIndex!=0){
                token[tokenIndex]='\0';
                matrix[matrixIndex]=atof(token);
                //printf("Stored: %d\n",matrixOne[matrixIndex]);
                tokenIndex=0;
                matrixIndex++;
            }
        }
        else if(byte==' ' && matrixHeight!=0){
            token[tokenIndex]='\0';
            matrix[matrixIndex]=atof(token);
            tokenIndex=0;
            matrixIndex++;
        }
        //printf("Token: %s, number matrix: %f\n" , token, matrix[matrixIndex-1]);
    }
}

This code is repeatedly run until the segmentation fault (each time the file it reads has different numbers). If you need any more code, just let me know. Any help would greatly be appreciated.

Upvotes: 2

Views: 362

Answers (2)

zvrba
zvrba

Reputation: 24584

How do you allocate the memory for the matrix? Equally important, do you free it? Anyway, a hint: compile your program with -g option to generate debug information, and learn how to use a debugger. Then you will find the problem, whereas we can just guess.

Upvotes: 0

Karlson
Karlson

Reputation: 3048

There are many problems that could cause the segmentation fault in the code you posted. Let me list a few:

  • As Jens pointed out how sure are you that the size of any token is actually < 500 characters
  • How sure are you that there are no run-on numbers like 1.32.4? ATOF could crash on this so use strtod instead
  • Use a debugger to run the program as macs pointed out that will give you the line that it crashed on and you can see the values of the variables so it gives you a better idea
  • Use safety checks when reading the input as the run-on numbers could be the least of the problems. Multiple '\n' characters could mess it up.
  • Since everything is text I would suggest you read using fscanf rather then building your own version.
  • Read up on Row major vs. Column Major formats depending on what the data might be you may not need to keep track of dimensions

Upvotes: 0

Related Questions