1

Having a problem where 2 pointers are passed to a method and right before the method returns I confirm with an if statement that the first is NULL, then the method returns and control goes back to main, I check this pointer again and it != NULL.

This happens in a while loop as I'm trying to iterate through a linked list so it ends up being an infinite loop (so I made the cin >> pause to check outputs before it goes crazy).

Inside a switch on a menu input in main I have:

case 'D':
{
    cout.setf(ios::left);
    cout << setw(20) << "Last Name" << setw(15) << "First Name"
            << setw(10) << "Priority" << setw(10) << "Arrival Time" << endl;
    NodeType<PatientType> *printer;
    PatientType *patientToPrint = new PatientType();
    ER.startIterate(printer, patientToPrint);
    while (printer != NULL) {
        PatientType::printPatient(*patientToPrint);
        ER.iterate(printer, patientToPrint);
        if (printer != NULL) {
            cout << "printer != NULL" << endl;
        } else {
            cout << "printer == NULL" << endl;
        }
        cout << "PAST ITERATE" << endl;
        int pause;
        cin >> pause;
    }
    cout << "OUT OF LOOP";
    delete printer;
    delete patientToPrint;
    break;
}

Where NodeType is a template class (so is LinkedListType, which is ER's type)

The startIterate function being called (in LinkedListType file) is:

template<class ItemType>
void LinkedListType<ItemType>::startIterate(NodeType<ItemType> *item, ItemType *itemToPrint) {
    if (head != NULL) {
        item = head;
        *itemToPrint = head->data;
    } else {
        item = NULL;
        itemToPrint = NULL;
    }
}

Where head is the head of the linked list (a private pointer to a NodeType)

Then the iterate function (in LinkedListType file also) is:

template<class ItemType>
void LinkedListType<ItemType>::iterate(NodeType<ItemType> *item, ItemType *itemToPrint) {
    NodeType<ItemType> *peek = item->next;
    if (item->next == NULL) {
        std::cout << "item->next == NULL" << std::endl;
    }
    if (peek != NULL) {
        std::cout << "peek != NULL" << std::endl;
        item = item->next;
        *itemToPrint = item->data;
    } else {
        std::cout << "peek == NULL" << std::endl;
        item = NULL;
        itemToPrint = NULL;
        if (item == NULL) {
            std::cout << "item == NULL" << std::endl;
        }
    }
}

After you add a couple nodes to the list and input 'D' (print list) the output works at first, printing out the head node's information, and then reads:

item->next == NULL
peek == NULL
item == NULL
printer != NULL
PAST ITERATE

Which doesn't seem to make sense, "printer" in main is the same as "item" in iterate, the output shows that upon leaving iterate it is NULL, and when arriving in main immediately after, it is not NULL.

After inputting an int for the pause variable, it keeps looping through continuously. What's more is it keeps printing out the head node's information each time too, which I also set to NULL at the end of iterate!

Instinct
  • 329
  • 3
  • 10
  • You are passing pointers by value. Your changes to the pointers passed will not be seen at the caller. – drescherjm Feb 11 '14 at 00:19
  • Not sure how to fix, startIterate and iterate both accept pointers as arguments but I need to change them somehow? – Instinct Feb 11 '14 at 00:35

1 Answers1

1
void LinkedListType<ItemType>::iterate(NodeType<ItemType> *& item, ItemType *itemToPrint)

See here:

Reason to Pass a Pointer by Reference in C++?

Community
  • 1
  • 1
PaulMcKenzie
  • 31,493
  • 4
  • 19
  • 38