0

次のタイプの構造(ネストあり)があります。

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

ただし、このコードは次の出力を生成します。

tarskin@5-PARA-11-0120:/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

何が起こっているのか誰にも分かりませんか?

-- 編集 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);
}

私が示したテストケースは、単一の化合物に対するものでした (つまり、i = 1)。

4

1 に答える 1

1

spectrum* spectra; /* Nested struct */によって参照されるメモリが適切に割り当てられていないか、(部分的に) 解放されていたと強く思います。

を使用してアプリを実行してみてくださいvalgrind

また (を使用している場合):および/またはオプションgccを使用すると、コンパイラgccの警告が表示されますか?-Wall-pedantic


次の mod/ を試して、app/ の動作が異なるかどうかを確認してください (for (j=0;..;..)この方法では to ループ間でスタックに触れません)。

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++) {
  ...
于 2012-04-19T14:05:09.953 に答える