我正在做一个C项目。只是我们有一个对健康至关重要的阅读计划。我可以添加患者、添加读数和删除患者。我有所有其他的工作,其中我将链接我的代码。我有一个设置故障,已在removePient中隔离。我试过gdb,但由于某种原因,它今天不想和我一起工作。
这是有问题的代码:
void removePatient(int patientID) {
int i, count;
Chartptr patientsChart;
Chartptr previousChart;
Chartptr currentChart;
CBuffptr healthTypeBuffer;
CBuffptr allHealthTypeBuffers[MAXREADINGS];
// if patient was found, remove the patient
patientsChart = getChart(patientID);
if (patientsChart != NULL) {
healthTypeBuffer = patientsChart->buffer;
if (healthTypeBuffer != NULL) {
// gather all the heath type buffers
count = 0;
for (i = 0; i < MAXREADINGS || healthTypeBuffer != NULL; ++i) {
allHealthTypeBuffers[i] = healthTypeBuffer;
healthTypeBuffer = healthTypeBuffer->next;
count++;
}
// free all the health type buffers
for (i = 0; i < count; ++i) {
free(allHealthTypeBuffers[i]);
}
}
// find the chart before specified patient chart
currentChart = patientList;
while (currentChart != patientsChart) {
previousChart = currentChart;
currentChart = currentChart->next;
}
// reorganize list, then free patient chart
previousChart->next = patientsChart->next;
free(patientsChart);
}
}
我相信我写的代码是可读的。
以下是上面代码中使用的一些结构声明:
/* One health type reading: timestamp + actual value */
typedef struct{
char timestamp[MAXTIME+1];
int value;
}Element;
/*
* Health type readings: linked list of Circular buffers
*/
typedef struct healthEntry* CBuffptr; /* pointer to a CircularBuffer */
typedef struct healthEntry{
int type; /* health data type (1-5) */
int start; /* index of oldest reading */
int end; /* index of most current reading */
Element reading[MAXREADINGS]; /* fixed array of readings */
CBuffptr next; /* pointer to next health type buffer */
}CircularBuffer;
/*
* Patient's health chart: ID + linked list of health type readings
*/
typedef struct chartEntry* Chartptr; /* pointer to a Chart */
typedef struct chartEntry{
int id; /* patient ID */
CBuffptr buffer; /* pointer to first health type buffer */
Chartptr next; /* pointer to next patient */
}Chart;
/* global declaration for start of the patient chart linked list */
extern Chartptr patientList;
虽然我没有仔细阅读您的大部分代码,但以下行对我来说似乎很可疑:
for (i = 0; i < MAXREADINGS || healthTypeBuffer != NULL; ++i) {
我怀疑你希望它是:
for (i = 0; i < MAXREADINGS && healthTypeBuffer != NULL; ++i) {
可能还有其他问题,但我非常确信上面的逻辑至少需要&&
。