2

Im getting several errors in valgrind referring to one section of my code. Basically I have a linked list of structs that acts as a queue. Here is my enqueue function:

PcbPtr enqPcb(PcbPtr *headofQ, PcbPtr process){
    PcbPtr c = *headofQ;
    PcbPtr d = c;
    if (!*headofQ) {
        *headofQ = process;
        return *headofQ;
    }
    while (c->next) {
        c = c->next;
    }
    c->next = process;
    return d;
}

PcbPtr is a pointer to the struct. Valgrind is giving me all sorts of stuff "Conditional jump or move depends on uninitialised value(s)" etc at the while loop. Its there anything noticeably wrong with this, or is beyond the scope of what Ive included? thanks

Milk
  • 647
  • 1
  • 6
  • 18
  • Unrelated to your problem, but why using intermediate variable `d`, when you can return `*headofQ` directly? – Some programmer dude Jun 07 '12 at 09:28
  • good question. I think it was a remnant of some debugging :) – Milk Jun 07 '12 at 09:39
  • 1
    @JoachimPileborg: Do you know, I have repeatedly seen beginning programmers use such intermediate variables. I don't know why. There's something in the psychology of many beginning programmers that provokes this: it's quite common. (On the other hand, the OP's style seems moderately mature, so the intermediate is not unlikely an artifact of prior modifications to the code in his case.) – thb Jun 07 '12 at 09:40
  • 3
    I think it's likely that the error is not in this code; most probably the list has been populated incorrectly. – Oliver Charlesworth Jun 07 '12 at 10:04
  • Did you ran valgrind using the `--track-origins=yes` option? It should have then told you where the uninitialised memory came from. – alk Jun 07 '12 at 10:58

1 Answers1

0

You might consider intialising the next member of what process is referring to (if not already done prior to calling enqPcb()):

PcbPtr enqPcb(PcbPtr *headofQ, PcbPtr process){
  PcbPtr c = *headofQ;
  PcbPtr d = c;
  process->next = NULL;

  ...
alk
  • 69,737
  • 10
  • 105
  • 255