0

错误消息是“已释放对象的校验和不正确 - 对象可能在被释放后被修改”。

这是给我带来麻烦的功能。'npart' 和 'parts' 是全局变量。'npart' 是一个 int 而 'parts' 是一个结构数组(参见下面的声明)。

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

Molecule *parts;

在这里,我将商转换为 int 以截断小数位。

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);
    }

}

我为“parts”数组分配内存并在 main 中调用 printXYZ() 函数:

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

请帮忙!

4

0 回答 0