我有一个结构,它被定义为将标头作为二进制数据文件的一部分。我用它来读一读,然后使用它的信息。之后我想用它来做另一次阅读。我是否需要释放它或其他任何东西才能再次使用它?
我遇到了一些内存错误,我认为这可能是问题所在。我包括代码,但它很粗糙,是我尝试调试此问题的当前结果。
void readSlices (struct header fileHead, unsigned long *offsets, FILE *fp, struct car **hashTable, int *tableSize){
struct TVehicle3D tempVehicle;
struct BlockHeaderData blockHead;
struct vertexNode *ptr;
int sliceNum = 1;
int i;
while (sliceNum <= fileHead.slicesStored) {
fseek (fp, offsets[sliceNum], SEEK_SET);
fread(&(blockHead),sizeof(blockHead), 1, fp);
printf ("Type: %d Size: %d\n", blockHead.objectType,blockHead.size );
while (blockHead.objectType != 88) {
if (blockHead.objectType == 86) {
printf ("Reading slice\n");
fread(&(tempVehicle),75, 1, fp);
if (*tableSize < tempVehicle.id) {
(*tableSize)++;
printf ("increasing tablesize (realloc)\n");
*hashTable = realloc(*hashTable, (*tableSize) * sizeof (struct car*));
};
if ((*hashTable)[tempVehicle.id].set == 0) {
(*hashTable)[tempVehicle.id].set = 1;
};
(*hashTable)[tempVehicle.id].sliceOut = sliceNum;
//printf ("size of table at slice #%d = %d\n",tempVehicle.id, *tableSize);
tempVehicle.centroid.x = ((tempVehicle.points[0].x)+(tempVehicle.points[1].x)+(tempVehicle.points[2].x)+(tempVehicle.points[3].x))/4;
tempVehicle.centroid.y = ((tempVehicle.points[0].y)+(tempVehicle.points[1].y)+(tempVehicle.points[2].y)+(tempVehicle.points[3].y))/4;
tempVehicle.centroid.z = ((tempVehicle.points[0].z)+(tempVehicle.points[1].z)+(tempVehicle.points[2].z)+(tempVehicle.points[3].z))/4;
ptr = (*hashTable)[tempVehicle.id].node;
printf ("Set ptr\n");
for (i = 0; i < sliceNum - (*hashTable)[tempVehicle.id].sliceIn; i++) {
printf ("Setting loop\n");
ptr = (*ptr).node;
};
printf ("Setting ptr xyz\n");
ptr = malloc (sizeof (struct vertexNode));
ptr->x = tempVehicle.centroid.x;
(ptr)->y = tempVehicle.centroid.y;
(ptr)->z = tempVehicle.centroid.z;
if (tempVehicle.id==1) printf ("centroid x: %d y: %d z: %d\n", tempVehicle.centroid.x, tempVehicle.centroid.y, tempVehicle.centroid.z);
}
else fseek (fp, ftell(fp) + blockHead.size, SEEK_SET);
fread(&(blockHead),sizeof(blockHead), 1, fp);
//printf ("Type: %d Size: %d\n", blockHead.objectType,blockHead.size );
};
sliceNum++;
};
}