void deleteElement(LinkedListElement<char> *&pending)
{
if (pending->Next) {
char value = pending->Next->Data;
pending->Data = value;
LinkedListElement<char> *temp = pending->Next;
pending->Next = pending->Next->Next;
delete temp;
}else{
pending = NULL;
//pending =nullptr;
delete pending;
}
}
Simple delete node in C++
what i want to ask is in the else statement, if the linked list is end , so i can just delete myselft, i pass value by reference(&), but it does not work
void deleteElement(LinkedListElement<char> *&pending)
{
if (pending->Next) {
char value = pending->Next->Data;
pending->Data = value;
LinkedListElement<char> *temp = pending->Next;
pending->Next = pending->Next->Next;
delete temp;
}else{
delete pending;
//pending = NULL;
}
}
i try this way, but it always give me error:
malloc: * error for object 0x100103b40: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
It should be
(your version is the other way around). Notice you’re first setting the pointer to
NULL, and after that deleting it, so you’re effectively callingdeleteon a null pointer. Which is a no-op – memory is not released.