I'm trying to display some object information and it seems to work when I use a pointer to the object, but not when I pass the pointer to a different function. Hopefully, the code will better explain:

struct pcb {
    int pid;
    string name;
}

void showData(pcb* PCBobject) {
    cout << "Number: " << PCBobject->pid << endl;
    cout << "Name: " << PCBobject->name << endl;
}

void outputQueue(queue<pcb*> readyQueue) {
    queue <pcb*> temp (readyQueue);
    for (int i = 0; i < readyQueue.size(); i++) {
        cout << "temp.front's number: " << temp.front()->pid << endl;
        cout << "temp.front's name: " << temp.front()->name << endl;
        showData(temp.front());
        temp.pop();
    }
}

The two cout statements in the outputQueue function display the correct information, but they're merely for testing purposes. I'd rather the information be displayed using a separate function: showData. However, showData throws a floating point exception and I can't figure out why. Am I passing the temp.front() pointer correctly?

Any help would be appreciated. Thanks.

The code looks like it ought to work.

Could you try something for me? (Look for compiler errors or warning around this area, it might be a clue).

(Note: if your first code tag says code=c++ you get line numbers and syntax highlighting)

void outputQueue(queue<pcb*> readyQueue) {
    queue <pcb*> temp (readyQueue);
    for (int i = 0; i < readyQueue.size(); i++) {
        pcb * ptf = temp.front();
        cout << "temp.front's number: " << ptf->pid << endl;
        cout << "temp.front's name: " << ptf->name << endl;
        showData(ptf);
        temp.pop();
    }
}

I added line 4 to make sure it explicitly has the type I want. Then I use it for the outputs on lines 5 and 6 and pass it to the function on line 7.

Please try that and let me know what if anything happens.

According to the reference I found, temp.front() actually returns a const TYPE & where TYPE is pcb * so we're getting something like a const pcb * & and I'm wondering if something related to the const is trying to 'help' you.

Despite of this barbaric by value queue parameter and absolutely unnecessary copying to the local temp variable, it's a correct code. It works if the queue was initialized properly.
Present outputQueue call context...

This article has been dead for over six months. Start a new discussion instead.