I´m currently playing around with linked lists today. I got everything to work just fine though I always seem to run into the same Problem,the destructor no matter what, it seems to always run into a NULL object when accessing the destructor in any form, throwing an "access rights violation Error"
"this->next" was "0xDDDDDDDD".
struct Liste {
Liste *next;
int content;
Liste() {
content = 0;
next = nullptr;
};
~Liste() {
if (next == nullptr) {
return;
}else if (next->next == nullptr){
delete next;
return;
}else {
next->~Liste();
}
};
};
I´m quite unsure and curious about what could be the source for this Error. It always occurs no matter the way i try to use delete on it. The solutions i found so far were: -simply call delete on the first element and it calls the destructor of the others -try to check if next in line IS Null but it doesn´t seem to filter it out at all -try to go through it iteratively but that too did run into ground zero -try to implement it in a class which did work just as fine as this did, up to the point i had to implement a deletion which ran into the exact same Error
Id be happy to receive some tips on how to fix/avoid this since not deleting it at all cant be a good idea, and i'm not sure at all what exactly went wrong in the code.