0

I'm having this strange segmentation fault. I'm trying to find if a patients id already exists in the list of patients using pointers. I think the code in question is:

#include <stdio.h>
#include <stdlib.h>
#include "health.h"

void addPatient(int patientID) {
printf("here1"); 
    Chart* patients_chart;

    // find the patient with id
    patients_chart = patientList;

    while(patients_chart == NULL || patients_chart->id == patientID) {
        patients_chart = patients_chart->next;
printf("here2");
    }

printf("here3");

    // if patient wasn't found, add new patient
    if (patients_chart == NULL) {
        Chart *new_chart;
printf("here4");
        // allocate and initialize new patient
        new_chart         = (Chart*)malloc(sizeof(Chart));
        new_chart->id     = patientID;
        new_chart->buffer = NULL;

        // insert new patient into list
        new_chart->next   = patientList;
        patientList       = new_chart;
printf("here5");
    }
}

The included health.h is just method declarations and structs. I will list them below, but please note that my assignment restricts me from modifying any of the code in health.h. I will also post my code at the very end.

/*
*   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;


extern Chartptr patientList;

I call the function in main with input like this one: 1,12:12:12,7,0

The 7 is the "command"

the 1 is the patient id in question

You can ignore the rest.

I understand how to find the patient, but I'm getting this annoying seg fault. Thank you for your time!

3
  • at which point do you have the segfault? Your searching loop condition does not seem fine. Commented Nov 28, 2013 at 15:45
  • Please don't cast the return value of malloc() in C. Commented Nov 28, 2013 at 15:46
  • @unwind thank you so much! You just proved my prof. wrong to some extent. Commented Nov 28, 2013 at 15:51

2 Answers 2

2

The following code is buggy:

while(patients_chart == NULL || patients_chart->id == patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}

You are advancing for as long as either the pointer is NULL or the pointer matches the patient ID. You're missing a negation there. Instead, use:

while(patients_chart != NULL && patients_chart->id != patientID) {
    patients_chart = patients_chart->next;
    printf("here2");
}
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that it did not fix the problem. Although, my looping logic does seem off.
This fixed my problem. But it came down to pointer dereferencing.
1
while(patients_chart == NULL || patients_chart->id == patientID) {
    patients_chart = patients_chart->next;
}

Here if Condition 1 (patients_chart == NULL) is true, then you do this:
patients_chart = patients_chart->next;

which is Null Pointer Dereferencing, thus causing Seg Fault.

1 Comment

This was it! patients_chart = patientList; if (patients_chart != NULL) { while(patients_chart->id != patientID) { patients_chart = patients_chart->next; } }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.