Destructor for a doubly-linked list that points to its value

You need to pair each new with exactly one delete. That is, you probably don’t want to delete prev (this node already was deleted) but you want to delete value. Well, I’d embed the value into the object and not point to it:

struct node
{
     node* prev;
     node* next;
     int   value;
};

If the value absolutely needs to be a pointer, I’d use a std::unique_ptr<int> (or, if you need to use C++ 2003, a std::auto_ptr<int>).

Leave a Comment