2016-01-24 46 views
0

我想隨機填充一個二維數組然後乘以它們,但由於某些奇怪的原因,當我運行我的代碼時,在最後一次迭代中,我出現了分段錯誤。我嘗試減少我傳遞的數量和一切,但錯誤仍然存​​在。這裏是我試圖執行的代碼,非常感謝任何幫助,謝謝。最後迭代分割錯誤

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

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

    FILE *matrixFile; 
    int n = atoi(argv[1]); // the number of matrices 
    int i, j; // must declare outside of for loop due to resolve C99 mode error 
    double arrA[n][n];// = CreateRandomMatrix(n); 
    double arrB[n][n]; 
    double sumArr[n][n]; 
    matrixFile = fopen("home/acolwell/Documents/CPE631_HW2_Number1/results.txt", "w+"); 

    printf("Usage: %s <size of nxn matrices>\n", argv[1]); 

    // randomly populate arrA and arrB 
    for(i = 0; i < n; i++) 
    { 
     printf("%d\n", i); 
     for(j = 0; j < n; j++) 
     { 
      printf("%4d", j); 
      arrA[i][j] = (double)rand()/(double)RAND_MAX; 
      arrB[i][j] = (double)rand()/(double)RAND_MAX; 
     } 
    } 

    printf("Exiting Matrix randomization"); 

    // multiply the matrices and write them to the file 
    for(i = 0; i < n; i++) 
    { 
     for(j = 0; j < n; j++) 
     { 
      sumArr[i][j] = arrA[i][j] * arrB[i][j]; 
      printf("Writing matrix "); 
      fprintf(matrixFile, "%0.3lf\n", sumArr[i][j]); 
     } 
    } 

    if(matrixFile) 
    { 
     fclose(matrixFile); 
    } 

    matrixFile = NULL; 

    return 0; 
} 
+1

據我所知,在C你不能使用非恆定創建數組時的值。 – cdonts

+1

@cirstts是的,你可以。它被稱爲'VLA' [可變長度數組],自c99,IIRC –

+0

@CraigEstey以來一直受到支持好奇! – cdonts

回答

1

這個錯誤將會歸結於註銷數組的末尾或未能打開文件。我建議運行gdb來看看當它運行您的程序,但看一眼不知你不是說有

"/home/acolwell/Documents/CPE631_HW2_Number1/results.txt" 

來寫,而不是

"home/acolwell/Documents/CPE631_HW2_Number1/results.txt" 
文件

我建議在調用fprintf之前檢查你的fopen調用的結果。

+0

謝謝,有時它只需要一個額外的一雙眼睛......現在我感覺很糟糕,因爲它非常簡單。 – user3596586

+0

它通常是和額外的一組眼睛或試圖解釋給別人通常是它需要找到它。它發生在我們所有人身上。 –

0

我剛編譯並測試了你的代碼。您提供的文件名稱不正確;你需要在「家」前面有一個「/」。

不知道需求是什麼,但寫matrixFile像矩陣:矩陣中的每一行後添加一個新行是「相乘」,每一個元素後不:

for(i = 0; i < n; i++) { 
    for(j = 0; j < n; j++) { 
     sumArr[i][j] = arrA[i][j] * arrB[i][j]; 
     printf("Writing matrix "); 
     fprintf(matrixFile, "%0.3lf ", sumArr[i][j]); 
    } 
    fprintf(matrixFile, "\n"); 
} 

而且,認真對待Craig Easley的評論。堆棧溢出可能發生,即使是在本網站的場所;)考慮在堆上動態分配矩陣。

1

如果n足夠大,您將使用VLA s產生堆棧溢出。我已使用您的代碼通過實驗驗證了這一點(例如,使用了5000的n)。

因此,您需要使用malloc從堆分配。但是,這需要一點改寫。

這裏有一個方法來使用堆分配 [使用一些輕微的掛羊頭賣狗肉]得到VLA的好處:

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

#define C(_arr) (double (*)[(size_t)(n)]) _arr 

void 
docalc(FILE *fout,int n,double arrA[n][n],double arrB[n][n],double sumArr[n][n]) 
{ 
    // must declare outside of for loop due to resolve C99 mode error 
    int i, 
    j; 

    // randomly populate arrA and arrB 
    for (i = 0; i < n; i++) { 
     printf("%d\n", i); 
     for (j = 0; j < n; j++) { 
      printf("%4d", j); 
      arrA[i][j] = (double) rand()/(double) RAND_MAX; 
      arrB[i][j] = (double) rand()/(double) RAND_MAX; 
     } 
    } 

    printf("Exiting Matrix randomization"); 

    // multiply the matrices and write them to the file 
    for (i = 0; i < n; i++) { 
     for (j = 0; j < n; j++) { 
      sumArr[i][j] = arrA[i][j] * arrB[i][j]; 
      printf("Writing matrix\n"); 
      fprintf(fout, "%0.3lf\n", sumArr[i][j]); 
     } 
    } 
} 

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

    FILE *matrixFile; 
    int n = atoi(argv[1]);    // the number of matrices 

    printf("Usage: %s <size of nxn matrices>\n", argv[1]); 

    matrixFile = fopen("/tmp/results.txt", "w+"); 
    if (matrixFile == NULL) { 
     perror("fopen"); 
     exit(1); 
    } 

    double *arrA = malloc(sizeof(double) * n * n); 
    double *arrB = malloc(sizeof(double) * n * n); 
    double *sumArr = malloc(sizeof(double) * n * n); 

    docalc(matrixFile,n,C(arrA),C(arrB),C(sumArr)); 

    if (matrixFile) 
     fclose(matrixFile); 

    matrixFile = NULL; 

    return 0; 
} 
+0

什麼?!沒有'free()'?褻瀆! – e0k

+0

已驗證此功能是否適用於'n = 5000'。 – e0k

+0

@ e0k是的,我也沒有爲'NULL'檢查'malloc'。所以[egads!],雙重褻瀆...... :-)對我來說,我通常不會從'main'執行'free',因爲它會有一點爭議,因爲程序會立即終止[而且它實際上更快不做他們 - 特殊情況]。如果我在一個庫函數[名爲'docalc']內部執行'malloc',我將執行'free'' –