typedef struct Halo* Halo;
That's a terrible idea, and probably the cause of your problems.
You have a global variable
Halo *halo_catalog;
so a struct Halo**
. But in
halo_catalog = (Halo *)calloc(halo_num, sizeof(struct Halo));
if (fread(halo_catalog, sizeof(struct Halo), halo_num, catalog_file) < 0) {
you use it as if it were a struct Halo*
.
Then you index
printf("%f\n", halo_catalog[10000]->x);
at a distance of 10000 * sizeof(struct Halo*)
bytes from where halo_catalog
points, and interpret the bytes at that location - which are part of the values of some float
s or int
s read in, not pointers - and try to access the x
component of whatever arbitrary location was the result of that misinterpretation.
You should
typedef struct Halo Halo;
and use halo_catalog[10000].x
to fix that issue.
Another issue is that fread
returns the number of items it has successfully read, if that is smaller than the requested number, it can still be a positive number.
Capture the return value of fread
, and use that to determine whether the reading was completely successful. Also, be sure that 10000 is a valid index before trying to print halo_catalog[10000].x
.