我正在研究一个C项目。正是这一点,我们有一个健康的重要阅读计划。我可以添加患者并添加读数并移除患者。我完成所有其他工作,其中我将链接我的代码。我遇到了一个我在removePatient中隔离的故障。我已经尝试了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;
答案 0 :(得分:4)
虽然我没有查看您的大部分代码,但以下行对我来说似乎很可疑:
for (i = 0; i < MAXREADINGS || healthTypeBuffer != NULL; ++i) {
我怀疑你想要它:
for (i = 0; i < MAXREADINGS && healthTypeBuffer != NULL; ++i) {
也可能存在其他问题,但我很确定上述逻辑至少要求&&
。