In my implementation of a linked-list, my helper function deleteNode(Node*)
that deletes inner class Node
instances is throwing a runtime error, by "triggering a breakpoint" in the Local Windows Debugger in VS 2015. I am being careful to match my new
and delete
operators. Does scope/reference play a role that I don't realize?
Even if there is a logic error in clear()
and deleteNode()
gets passed nullptr
, it shouldn't throw an error to delete nullptr
, and then assign nullptr
to itself right? What is the issue with that delete?
class LinkedList {
public:
LinkedList() : head(nullptr) {}
~LinkedList() { clear(); }
void push_front() {
head = createNode(head);
}
void clear() {
Node* current_node = head;
while (current_node != nullptr) {
Node* next_node = current_node->next; // buffer
deleteNode(current_node);
current_node = next_node;
}
}
private:
struct Node {
Node(Node* next)
: next(next) {}
Node* next;
};
Node* head;
Node* createNode(Node* next) {
return new Node(next);
}
void deleteNode(Node*& toDelete) {
delete toDelete; // ***VS 2015 puts breakpoint here***
toDelete = nullptr;
}
};
int main() {
auto A = LinkedList();
A.push_front();
A.clear();
return 0;
}
I have removed all attributes and methods not relevant to the error. This dummy code still throws the same error.