#Anyone know what is causing this error? I have tried so many different things

27 messages · Page 1 of 1 (latest)

lusty spearBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

celest tartan
#

void createNoList(vector<PCBNoList>& pcb, int p) {
PCBNoList child;
child.parent = p;
if (pcb[p].firstChild == -1) {
pcb[p].firstChild = pcb.size();
}
else {
int youngest_sibling = pcb[p].firstChild;
while (pcb[youngest_sibling].younger != -1) {
youngest_sibling = pcb[youngest_sibling].younger;
}
pcb[youngest_sibling].younger = pcb.size();
child.older = youngest_sibling;
}
pcb.push_back(child);
}

sand lodge
#

You are accessing an element out of range of the vector, the message says very clearly.

#

Debug to see the offending code and values.

celest tartan
sand lodge
#

Look at the values.

celest tartan
#

how would while (pcb[youngest_sibling].younger != -1) trip an out of bounds error, when its just not equivalent to

sand lodge
#

The index can be too big.

celest tartan
#

struct PCBNoList {
int parent;
int firstChild;
int younger;
int older;
};

#

might have to do with firstChild being unitialized

#

nvm

#

bc that code actually initializes it

#

struct PCBNoList {
int parent;
int firstChild;
int younger;
int older;
};

#

thats the whole program if anyone can take a crack at it I would be very grateful I'm stumped

acoustic oxide
#

In line 81 you call createNoList with argument pcbV2 which is an empty vector. Then in line 31 you do if (pcb[p].firstChild == -1), but pcb[p] does not exist, because pcb is empty.

#

And thus it crashes.

celest tartan
acoustic oxide
#

I don't know, that's for you to decide. You could do pcb.push_back(p); to add p to the vector. If that makes sense for what you're trying to do is another question. I didn't look closely enough to figure out the goal.

celest tartan
# acoustic oxide I don't know, that's for you to decide. You could do `pcb.push_back(p);` to add ...

Well it's for an operating systems course, and it's supposed to simulate the creation and destruction of process control blocks with and without linked lists, this is without linked lists, where each has a parent and children, I did what I thought was logically correct in the second create function but now I just need to figure out what to do when there is no parent, maybe create another create method for initializing?

#

fixed it

#

void createNoList(vector<PCBNoList>& pcb, int p) {
PCBNoList child;
if (pcb.size() <= 0)
{
child.parent = -1;
child.firstChild = -1;
child.younger = -1;
child.older = -1;
pcb.push_back(child);
}
child.parent = p;

if (pcb.at(p).older == -1) {
    pcb[p].firstChild = pcb.size();
}
else {
    int youngest_sibling = pcb[p].firstChild;
    while (pcb[youngest_sibling].younger != -1) {
        youngest_sibling = pcb[youngest_sibling].younger;
    }
    pcb[youngest_sibling].younger = pcb.size();
    child.older = youngest_sibling;
}
pcb.push_back(child);

}

#

Thank you for the help

#

!solved

lusty spearBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity