2012-07-14 95 views
0

我有在科學格式號的兩列多個數據文件,如下所示:讀值會導致段故障,C

-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 
    ... 

第一列是x數據,第二個是y數據。這些列由<tab>字符分隔。文件中的行數從300到400不等。我編寫了一個程序來讀取文件中的值,並使用第100和第200個點計算梯度。從這個梯度我需要計算x截距(我使用第150個點)。這是源:

#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); 
} 

該程序編譯沒有錯誤或警告,但在執行時產生一個分段錯誤(在Ubuntu Linux)。我一直在仔細研究這個問題並且仍然無法發現問題。任何幫助/意見將不勝感激。非常感謝你。

+0

它在調試器中運行時會在哪裏崩潰? – 2012-07-14 22:21:33

+0

啓用核心轉儲,使用gdb來查找哪條線或代碼發生故障。那麼你可以縮小它的範圍。 – Aftnix 2012-07-14 22:29:21

+0

您可以計算完整的線性迴歸,而不是以您所採用的方式計算梯度,而是根據整個數據集給出斜率和截距。 – 2012-07-14 22:31:51

回答

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

你想在malloc(2*sizeof(float*))那裏。或更好的malloc(2*sizeof *entry);

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

分配兩個float的空間來存儲兩個float *。如果您的平臺sizeof(float) == sizeof(float *)屬實,那麼這將起作用。是嗎?無論如何它應該被糾正。

此外,初始化for(i=0; i<size; i++) entry[1][i]==0.0;只是將未初始化的內存與0.0進行比較。