Reputation: 166
I'm working on a C project. The just of it is that we have a health vital reading program. I can add patients and add readings and remove patients. I have all the rest working, of which I'll link my code. I'm having a set fault that I've isolated in removePatient. I've tried gdb but for some reason it doesn't want to work with me today.
Here is the code in question:
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);
}
}
I believe I wrote the code to be fairly readable.
Here are some of the struct declarations that are used in the above code:
/* 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;
Upvotes: 0
Views: 162
Reputation: 4490
While I didn't look through most of your code, the following line seems suspicious to me:
for (i = 0; i < MAXREADINGS || healthTypeBuffer != NULL; ++i) {
I suspect that you want it to be:
for (i = 0; i < MAXREADINGS && healthTypeBuffer != NULL; ++i) {
There may be other issues too, but I'm pretty sure that the logic above at least calls for &&
.
Upvotes: 4