The error message is "incorrect checksum for freed object - object was probably modified after being freed."

This is the function that is giving me trouble. 'npart' and 'parts' are global variables. 'npart' is an int while 'parts' is an array of structs (see declaration below).

typedef struct {
    int bSites;
    int rotState; // Direction
    int pos[2]; // Array index and layer
} Molecule;

Molecule *parts;

Here, I cast the quotient to an int to truncate the decimal places.

void printXYZ() {

    fprintf(fp, "%d\n\n", npart);
    for (int i = 0; i < npart; i++){
        char type = ((int) xyz[i][0]) == 3 ? 'S' : 'O';

        double x, y, z;
        z = parts[i].pos[0];
        y = ((int) parts[i].pos[1] / size) * 1.73205080757 / 2.0;
        if (((int) parts[i].pos[1] / size) % 2 == 0)
            x = (double)(parts[i].pos[1] % size);
        else
            x = ((double)(parts[i].pos[1] % size)) / 2.0;
        printf("%c\t%f\t%f\t%f\n", type, x, y, z);
        fprintf(fp, "%c\t%f\t%f\t%f\n", type, x, y, z);
    }

}

I allocate memory for the 'parts' array and call the printXYZ() function in main:

parts = (Molecule*) malloc(npart * sizeof(Molecule));

Please help!

0

There are 0 best solutions below