2012-04-19 48 views
0

我有一個結構(具有嵌套)變化:值存儲在C結構「神奇」以下類型的自己

typedef struct { 
    float precursor_mz; 
    float precursor_int; 
    int scan; 
    float time; 
    spectrum* spectra; /* Nested struct */ 
    int array_length; 
    int mz_length; 
    int int_length; 
    char* mz_binary; 
    char* int_binary; 
    int hits; 
} compound; 
typedef struct { 
    float mz_value; 
    float int_value; 
    int peaks; 
} spectrum; 

我改變這個結構,允許我使用的qsort,後我將它存儲爲我自己的'類型'。幾行後面的代碼我希望循環結構,但不知何故值更改沒有我訪問他們(之間)。代碼片斷如下:

// The transformating & qsort chunk 
    for (i = 0; i < compounds->hits; i++) { 
     spectrum test[(compounds+i)->spectra->peaks]; 
     for (j = 0; j < (compounds+i)->spectra->peaks; j++) { 
      test[j] = *((compounds+i)->spectra+j); 
     } 
     qsort(test,(compounds+i)->spectra->peaks,sizeof(spectrum),compare_mz); 
     for (j = 0; j < (compounds+i)->spectra->peaks; j++) { 
      ((compounds+i)->spectra+j)->mz_value = test[j].mz_value; 
      ((compounds+i)->spectra+j)->int_value = test[j].int_value; 
      if (j < 10) { 
       printf("%i %i\t", i, j); 
       printf("%f %f\n",((compounds+i)->spectra+j)->mz_value, ((compounds+i)->spectra+j)->int_value); // Here values are still correct 
      } 
     } 
    } 


/* Summing values that are in 'mass-tolerance' of each other */ 
    float int_total; 
    float mz_int_total; 
    for (i = 0; i < compounds->hits; i++) { 
     counter = 0; 
     printf("---\n"); 
     for (j = 0; j < (compounds+i)->spectra->peaks; j++) { 
      lower_mass = ((compounds+i)->spectra+j)->mz_value - 0.05; //args->mass_tolerance; 
      upper_mass = ((compounds+i)->spectra+j)->mz_value + 0.05; //args->mass_tolerance; 
      if (j < 10) { 
       printf("%i %i\t", i , j); 
       printf("%f %f\n",((compounds+i)->spectra+j)->mz_value, ((compounds+i)->spectra+j)->int_value); // Here values are borked 
      } 
      // Rest of the code chopped off as it should be irrelevant 

但此代碼產生以下輸出:

[email protected]:/data/programming/C/Compound_Spectra$ ./Run -f ../PeptMoiety/32757_p_01.mzML -c 1 
0 0 168.858765 32489.994141 
0 1 168.960327 72930.046875 
0 2 169.039993 4924.188477 
0 3 169.913681 85340.171875 
0 4 169.932312 2406.798096 
0 5 171.000320 345949.593750 
0 6 171.007950 1034718.312500 
0 7 171.034088 882886.562500 
0 8 171.034378 58554.589844 
0 9 171.056320 871035.500000 
--- 
0 0 168.858765 32489.994141 
0 1 168.960327 72930.046875 
0 2 169.039993 4924.188477 
0 3 169.913681 85340.171875 
0 4 0.000000 0.000000 
0 5 169.932312 2406.798096 
0 6 171.007950 1034718.312500 
0 7 0.000000 0.000000 
0 8 0.000000 0.000000 
0 9 0.000000 0.000000 

沒有人有任何想法可能是什麼回事?

- EDIT 1 -

ALK請求compare_mz的代碼,其中如下:

int 
compare_mz (const void *a, const void *b) 
{ 
    const spectrum *fa = (const spectrum *) a; 
    const spectrum *fb = (const spectrum *) b; 

    return (fa->mz_value > fb->mz_value) 
      -(fa->mz_value < fb->mz_value); 
} 

測試用例,我表明是爲單一化合物(所以我= 1)。

+1

可以簡化測試案例?現在有太多東西在進行着。 – 2012-04-19 13:00:39

+1

嗯..你有相當多的調試在那裏做。我會將數據集簡化爲兩個無序的項目,然後逐步執行調試程序,並仔細記錄值和索引。隨着對完整代碼的熟悉,編譯以及知道我在做什麼,您應該能夠找到問題。 – 2012-04-19 13:01:37

+0

你還可以提供'compare_mz'的代碼嗎? – alk 2012-04-19 13:09:35

回答

1

我強烈地認爲spectrum* spectra; /* Nested struct */引用的內存沒有被正確地分配或者被部分釋放。

嘗試使用valgrind運行您的應用。

而且(如果你使用的是gcc):你如果使用gcc-Wall和/或-pedantic選項得到任何編譯警告?


試試下面的MOD /,看看應用程序/表現不同(堆棧將不間觸及到for (j=0;..;..)循環這樣):

float int_total; 
float mz_int_total; 
spectrum test[(compounds+i)->spectra->peaks]; 

for (i = 0; i < compounds->hits; i++) { 
    ... 

/* Summing values that are in 'mass-tolerance' of each other */  
for (i = 0; i < compounds->hits; i++) { 
    ... 
+0

@Bas揚森:如果移動declartion '光譜測試[(化合物+ I) - > spectra->峯]會發生什麼;' 出來的' for(;;)'上下文嗎? – alk 2012-04-19 14:08:51

+0

vaglrind給出了它的全部清除(除了與代碼中其他地方的膨脹有關的已知問題),並且沒有任何警告出現在-Wall中。不知道究竟是什麼迂腐,我會看起來。我遇到這個問題時遇到的第一個反射就是釋放(化合物+ i) - >光譜並新分配它,但是這不會改變任何事情,它不應該像元素必須分配爲浮動元素之前和之後一樣(除非我在那裏犯概念錯誤)。 – 2012-04-19 14:09:53

相關問題