Error when using Nodes in C++
Code:
void LIST::insertEvent(EVENT *e)
{
LLNODE* curr = front;
LLNODE* newn = new LLNODE(e);
LLNODE* old = NULL;
if (curr == NULL)
{
front = newn;
}
do {
old = curr;
curr = curr->next; <------------------------------------------------------------------------------------the error spot
} while ((curr->ev->getprocessTime() <= newn->ev->getprocessTime()) && curr != NULL);
if (old == NULL)
{
newn->next = front;
front = newn;
}
old->next = newn;
newn->next = curr;
}
is my function, and
Code:
class LLNODE {
friend class LIST; //"LIST is my friend!"
private:
LLNODE *next;
EVENT *ev;
public:
LLNODE(EVENT *e);
~LLNODE();
};
is the header function for it. At the line "curr = curr->next;" (in the do-while loop), there is a "Unhandled exception thrown: read access violation. curr was nullptr." error. Why is the exception being thrown there?
Re: Error when using Nodes in C++
Well, because simply your front data member is initialized as NULL. And then the current pointer is set as front (NULL).
It'd would be interesting to see how the front member is being assigned.
Re: Error when using Nodes in C++
When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#'.
Cheers!
Re: Error when using Nodes in C++
Code:
if (curr == NULL)
{
front = newn;
}
do {
old = curr;
curr = curr->next;
As indicated by AvDav, when curr is null, you set front to newn but don't set curr. Then in the do loop you deference curr which is null so the exception is thrown.