lundi 9 septembre 2019

Unhandled exception-Bad memory allocation

Bad memory allocation. Unhandled exception. Cannot identity the cause.

Tried try and catch and also std::nothrow with the new operator.

void LinkedList::operator -=(LinkedList& two) // first list =first list-second list. {

if (head == NULL && tail == NULL) //check list is empty.
{

    return;
}

for (this->current = this->head; this->current != NULL; this->current = this->current->getNext()) //for loop that runs for list1
{

    for (Node* second = two.head; second != NULL; second = second->getNext()) //for loop that runs for list2
    {

        string name = this->current->getData().get_name(); //get name of list1
        string name1 = second->getData().get_name(); //get name of list2
        int score = this->current->getData().get_score(); //get score of list1
        int score1 = second->getData().get_score();// get score of list2 


        if (name == name1 && score == score1) //if score and name match between the 2 lists
        {
            this->tail = this->current->getPrev(); //tail is the matched last node's of list1 prev

            while(this->current!=NULL) //run a loop till the current is NULL
            {
                Node* p;
                p = this->current;
                //cout << p->getData().get_name();

                this->current = this->current->getNext(); //move current to next, so we delete old current.     
                //cout << current->getData().get_name();
                delete p;       
            }
                this->tail->setNext(NULL); //set next of tail as NULL
                return;

        }
    }

}

}

ostream& operator <<(ostream& out, const LinkedList& list) {

Node * ptr =  list.head;

cout << ptr->getData().get_name();
for (;ptr != NULL && ptr->getNext() != NULL;ptr = ptr->getNext())
out << "("<<ptr->getData().get_name()<<","<<ptr->getData().get_score()<<")"<<endl;
out << "(" << ptr->getData().get_name() << "," << ptr->getData().get_score() << ")" << endl;
//cout << "tail in <<" << list.tail->getData().get_name();
    return out;

}

No program crash.

Aucun commentaire:

Enregistrer un commentaire