Khadija
Khadija

Reputation: 155

Reading values from file into array causes segmentation fault, C

I have multiple data files with two columns of numbers in scientific format as shown below:

   -6.000000E-2 0.000000E+0
    1.060000E+0 0.000000E+0
    2.510000E+0 0.000000E+0
    2.700000E+0 5.000000E-2
    2.735000E+0 5.000000E-2
    2.755000E+0 1.000000E-1
    2.775000E+0 1.500000E-1
    2.785000E+0 1.500000E-1
    2.790000E+0 2.000000E-1
    2.805000E+0 2.500000E-1
    2.810000E+0 3.500000E-1
    2.815000E+0 4.000000E-1
    2.820000E+0 5.000000E-1
    2.825000E+0 5.500000E-1
    2.835000E+0 6.000000E-1
    2.835000E+0 7.000000E-1
    2.840000E+0 8.000000E-1
    2.840000E+0 9.000000E-1
    2.850000E+0 9.500000E-1
    2.850000E+0 1.050000E+0
    2.855000E+0 1.200000E+0
    2.860000E+0 1.300000E+0
    2.865000E+0 1.400000E+0
    2.870000E+0 1.450000E+0
    2.870000E+0 1.600000E+0
    2.875000E+0 1.650000E+0
    2.880000E+0 1.800000E+0
    2.880000E+0 1.900000E+0
    2.885000E+0 2.000000E+0
    2.885000E+0 2.150000E+0
    2.890000E+0 2.200000E+0
    2.895000E+0 2.300000E+0
    ...

The first column is the x-data, the second is the y-data. The columns are separated by a <tab> character. The number of lines in the file varies from 300 to 400. I wrote a program to read the values from the file and calculate the gradient using the 100th and 200th point. From that gradient i need to calculate the x-intercept (which i use the 150th point). This is the source:

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

float **make_array(int size);
void cleanup(float **array, FILE *filehandle);

int main(int argc, char **argv)
{   
    float **entry;  //2D array to hold data; entry[0][i] holds the x_i while entry[1][i] holds y_i.
    FILE *infile;
    infile=fopen(argv[1], "r");
    if(infile=NULL){
        printf("Unable to open inpufile");
        exit(1);
    }

    char c;
    int i, numlines=0, ret;

    //count number of lines in file
    while((c=(fgetc(infile))!=EOF))
        if(c=='\n')
            numlines++;
    printf("Number of lines: %d", numlines);

    //array to store values from file
    entry=make_array(numlines);
    rewind(infile);

    //read values into array, ensure correct number of arguments read
    for(i=0; i<numlines; i++){
        fscanf(infile, " %g\t%g\n", &entry[0][i], &entry[1][i]);
        if(ret!=2){
            printf("reading incorrect number of inputs from file.. Terminating");
            cleanup(entry,infile);
            exit(3);
        }
    }
    //dump array contents to screen to check
    for(i=0;i<numlines;i++)
        printf("%f\t%f\n", entry[0][i], entry[1][i]);

    //find gradient of the data between the 100th and 200th point
    float gradient = (entry[1][200]-entry[1][100])/(entry[0][200]-entry[0][100]);

    // use the 150th point (midpoint) and the gradient to calculate the x-intercept
    float vt = (entry[1][150]/gradient) - entry[0][150];
    printf("\n Threshold Voltage: %f\n", vt);

    cleanup(entry, infile);
return 0;
}

float **make_array(int size)
{   
    int i;
    float **entry = malloc(2*sizeof(float));
    if(entry==NULL) exit(2);
    entry[0] = malloc(size*sizeof(float));
    if(entry[0]==NULL) exit(2);
    for(i=0; i<size; i++) entry[0][i]==0.0;
    entry[1] = malloc(size*sizeof(float));
    if(entry[1]==NULL) exit(2);
    for(i=0; i<size; i++) entry[1][i]==0.0;
return entry;
}

void cleanup(float **array, FILE *filehandle)
{   free(array[0]);
    free(array[1]);
    free(array);
    fclose(filehandle);
}

The program compiles without errors or warning but produces a segmentation fault when executed (on Ubuntu Linux). I have been looking through this for quite sometime and still am unable to spot the problem. Any help/comments would be greatly appreciated. Thank you very much.

Upvotes: 0

Views: 682

Answers (2)

Arne
Arne

Reputation: 2146

float **entry = malloc(2*sizeof(float));

allocates the space of two float to store two float *. That will work, if on your platform sizeof(float) == sizeof(float *) is true. is it? Anyway it should be corrected.

Besides, the initialization for(i=0; i<size; i++) entry[1][i]==0.0; does nothing but compares uninitialized memory with 0.0.

Upvotes: 1

Daniel Fischer
Daniel Fischer

Reputation: 183968

float **entry = malloc(2*sizeof(float));

You want to malloc(2*sizeof(float*)) there. Or better malloc(2*sizeof *entry);.

Upvotes: 1

Related Questions