我有一个以下类型的结构(带有嵌套):
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)。
最佳答案
我强烈假设 spectrum* spectra; /* Nested struct */
引用的内存未正确分配或已(部分)释放。
尝试使用 valgrind
运行您的应用程序.
另外(如果您使用 gcc
):如果使用 gcc
,您是否会收到任何编译器警告?的-Wall
和/或 -pedantic
选项?
尝试以下 mod/并查看 app/的行为是否不同(以这种方式,在 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++) {
...
关于c - 存储在 C 结构 'magically' 中的值会自行更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10228465/