2016-03-09 46 views
1

我正在寫一個代碼來存儲來自文本文件的數據。代碼編譯,但它似乎停止運行在ReadPanelBlock函數for循環。它的結構爲: CHBStructure包含CHBPanels其中包含CHBOpenings。這些功能是爲了從一個輸入文本文件中讀取而創建的,我將這些文件組織成數據塊以便於閱讀。結構體處理和結構體陣列

typedef struct _open 
{ 
    int id; 
    double length; 
    double height; 
    double origX; 
    double origY; 
    int frames; 

    double thickness; 
    double E; 
    double v; 
}CHBOpening; 

typedef struct _panels 
{ 
    int id; 
    double length; 
    double height; 
    double origX; 
    double origY; 
    double origZ; 
    double angle; 
    int nOpenings; 
    int nReinforcement; 
    double *xReinf; 
    double sx; 
    double xReinf0; 

    CHBUnit *chb; 
    CHBOpening *openings[]; 
}CHBPanel; 

typedef struct _chb 
{ 
    int nStories; 
    int nModes; 
    int nIter; 
    int nPanels; 
    CHBPanel *panels[]; 
}CHBStructure; 

int ReadPanelBlock (FILE *fp, CHBStructure *S) 
{ 
    CHBOpening *openings = malloc(sizeof(CHBOpening)); 
    *S->panels = malloc(S->nPanels*sizeof(CHBPanel)); 
    for (i=0; i<S->nPanels; i++) 
    { 
     fscanf(fp,"%d",&S->panels[i]->id); 
     fscanf(fp,"%lf",&S->panels[i]->length); 
     fscanf(fp,"%lf",&S->panels[i]->height); 
     fscanf(fp,"%lf",&S->panels[i]->angle); 
     fscanf(fp,"%lf",&S->panels[i]->origX); 
     fscanf(fp,"%lf",&S->panels[i]->origY); 
     fscanf(fp,"%lf",&S->panels[i]->origZ); 
     fscanf(fp,"%d",&S->panels[i]->nOpenings); 
     if (S->panels[i]->nOpenings > 0) 
     { 
      for (j=0; j<S->panels[i]->nOpenings;j++) 
      { 
       openings = S->panels[i]->openings[j]; 
       fscanf(fp,"%d",&openings->id); 
       fscanf(fp,"%lf",&openings->length); 
       fscanf(fp,"%lf",&openings->height); 
       fscanf(fp,"%lf",&openings->origX); 
       fscanf(fp,"%lf",&openings->origY); 
      } 
     } 
    } 
    return 1; 
} 

EDIT 1:我嘗試跟蹤在由每行打印文本中發生錯誤,它似乎在ReadPanelBlock功能的第一fscanf函數崩潰。

回答

2

訪問S->panels[i]->openings[j]的因子是UB,因爲您沒有爲該數組分配空間。

CHBOpening *openings = malloc(sizeof(CHBOpening)); 

將導致內存泄漏,因爲沒有代碼將釋放該存儲器由於這樣的事實,該地址將被jfor與丟失:

if (S->panels[i].nOpenings > 0) 
    { 
     S->panels[i].openings = malloc(sizeof(CHBPanel)*S->panels[i].nOpenings); 

     // inner for 
    } 

預用於openings指針與分配的空間代碼

openings = S->panels[i]->openings[j]; 

你能避免有一個指針和寫

 for (j=0; j<S->panels[i].nOpenings;j++) 
     { 
      fscanf(fp,"%d",&S->panels[i].openings[j].id); 
      fscanf(fp,"%lf",&S->panels[i].openings[j].length); 
      fscanf(fp,"%lf",&S->panels[i].openings[j].height); 
      fscanf(fp,"%lf",&S->panels[i].openings[j].origX); 
      fscanf(fp,"%lf",&S->panels[i].openings[j].origY); 
     } 

在你的結構CHBPanel *panels[];CHBOpening *openings[];必須是:

CHBPanel *panels; 
CHBOpening *openings; 

最後但並非最不重要的事情:你必須學習一些關於指針和->.使用。

您的代碼應該是這樣的:

int ReadPanelBlock (FILE *fp, CHBStructure *S) 
{ 
    int i,j; 
    S->panels = malloc(S->nPanels*sizeof(CHBPanel)); 
    for (i=0; i< S->nPanels; i++) 
    { 
     fscanf(fp,"%d",&S->panels[i].id); 
     fscanf(fp,"%lf",&S->panels[i].length); 
     fscanf(fp,"%lf",&S->panels[i].height); 
     fscanf(fp,"%lf",&S->panels[i].angle); 
     fscanf(fp,"%lf",&S->panels[i].origX); 
     fscanf(fp,"%lf",&S->panels[i].origY); 
     fscanf(fp,"%lf",&S->panels[i].origZ); 
     fscanf(fp,"%d",&S->panels[i].nOpenings); 
     if (S->panels[i].nOpenings > 0) 
     { 
      S->panels[i].openings = malloc(sizeof(CHBOpening)*S->panels[i].nOpenings); 

      for (j=0; j<S->panels[i].nOpenings;j++) 
      { 
       fscanf(fp,"%d",&S->panels[i].openings[j].id); 
       fscanf(fp,"%lf",&S->panels[i].openings[j].length); 
       fscanf(fp,"%lf",&S->panels[i].openings[j].height); 
       fscanf(fp,"%lf",&S->panels[i].openings[j].origX); 
       fscanf(fp,"%lf",&S->panels[i].openings[j].origY); 
      } 
     } 
    } 
    return 1; 
} 
+0

我試圖插入之前剛內malloc的線,你建議,但現在報告無效使用可變數組成員。 –

+0

這有助於很多!語法上沒有問題,但程序仍然崩潰。我做了一些檢查,似乎文件指針不再讀取文件中的值,因此結構開放的成員的值爲0.0。該程序也不會返回到外部循環,並在內部循環的最後一次迭代中停止進程。有什麼想法嗎?再次感謝您的幫助! –

+0

@CarlChesterRagudo首先猜測:'S> nPanels = 1',你必須檢查數據是如何組織到源文件中的。用您的代碼和文件內容樣本發佈一個新問題。 – LPs