Code:
struct LinkedList {
int someData;
LinkedList* next;
LinkedList() : next(0) {}
~LinkedList() {delete next;}
};
void someFunction() {
LinkedList list;
list.next = new LinkedList;
list.next->next = new LinkedList;
list.next->next->next = new LinkedList;
// And so on...
}
Am i correct to say that this code does not leak memory? When list scopes, it should call its destructor, which calls the destructor of next, which calls the destructor of next, and so on, until delete 0 is called.