I was looking through other questions, but couldn't find any related to my problem. Basically I was writing a code to read some text files with data and process it later on. But I'm facing some weird problem that the values in array keeps on changing. Below I've given two 'for' loops, one where I read the values from file pointer and check it immediately with printf statement. The next 'for' loop is where I again check the values stored and for some reason it is different, with some random values.
for (int i = 0; i < ndihedrals; ++i)
{
fscanf (readdihedral, "%d %f %d %d %d %d\n", &sino_dih[i], &angle_dih[i], &atom1_dih[i], &atom2_dih[i], &atom3_dih[i], &atom4_dih[i]);
printf("i: %d ==> %d %f %d %d %d %d\n", i, sino_dih[i], angle_dih[i], atom1_dih[i], atom2_dih[i], atom3_dih[i], atom4_dih[i]);
}
for (int i = 0; i < ndihedrals; ++i)
{
printf("test2: i: %d ==> %d %f %d %d %d %d\n", i, sino_dih[i], angle_dih[i], atom1_dih[i], atom2_dih[i], atom3_dih[i], atom4_dih[i]);
}
Here is the output from first for loop: (There are more lines, I'm just showing the first few lines of output from both for loops. I got these by putting sleep(1); statement after printf)
i: 0 ==> 1 -46.265598 696 698 699 700
i: 1 ==> 2 176.755005 696 698 699 701
i: 2 ==> 3 -56.561798 698 699 701 702
i: 3 ==> 4 168.240997 700 699 701 702
i: 4 ==> 5 172.516998 699 701 702 703
i: 5 ==> 6 -69.096497 699 701 702 704
i: 6 ==> 7 143.464005 701 702 704 705
i: 7 ==> 8 -98.824898 703 702 704 705
i: 8 ==> 9 149.878998 702 704 705 706
i: 9 ==> 10 -70.438004 702 704 705 707
i: 10 ==> 11 122.935997 704 705 707 708
Here is the output from second for loop:
test2: i: 0 ==> 1 0.000000 4533 4440 4441 4443
test2: i: 1 ==> 2 0.000000 4534 4442 4441 4443
test2: i: 2 ==> 3 0.000000 4535 4441 4443 4444
test2: i: 3 ==> 4 0.000000 4536 4441 4443 4444
test2: i: 4 ==> 5 0.000000 4537 632 633 635
test2: i: 5 ==> 6 0.000000 4538 634 633 635
test2: i: 6 ==> 7 0.000000 4539 633 635 636
test2: i: 7 ==> 8 0.000000 4540 633 635 636
test2: i: 8 ==> 9 0.000000 4541 635 636 638
test2: i: 9 ==> 10 0.000000 4542 637 636 638
test2: i: 10 ==> 11 0.000000 4543 636 638 639
Here is the declaration part for the code, I tried the code with both malloc and calloc.
sino_dih = (int *) calloc (natoms, sizeof (int));
atom1_dih = (int *) calloc (natoms, sizeof (int));
atom2_dih = (int *) calloc (natoms, sizeof (int));
atom3_dih = (int *) calloc (natoms, sizeof (int));
atom4_dih = (int *) calloc (natoms, sizeof (int));
angle_dih = (float *) calloc (natoms, sizeof (float));
I've attached the full code here (link: https://wetransfer.com/downloads/06c55057ecb6dade8af6407addadec6920180419090057/585c26)
My GCC version: 7.3.1 20180312