我有以下类型的结构(带嵌套):
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)。
发布于 2012-04-19 14:05:09
我强烈地认为,spectrum* spectra; /* Nested struct */引用的内存没有被正确分配,或者(部分)被释放。
尝试使用valgrind运行您的应用程序。
此外(如果您正在使用gcc):如果使用gcc的-Wall和/或-pedantic选项,您会收到编译器警告吗?
尝试下面的mod/并查看应用程序/的行为是否不同(这样不会在to 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++) {
...https://stackoverflow.com/questions/10228465
复制相似问题