I have an array a size of N. All the array elements are singly linked lists created with shared pointers. These linked lists consist of structs called S. These S elements have some data and a shared_ptr pointing to the next S element. The last S element in linked list of array[i] points to nullptr.
When I want to clear the array, how can I make the shared pointers to release all the memory?
A simple for loop doing array[i] = nullptr; won't do it? Or will it?
I've already tried doubly linked lists with shared_ptr to next S and normal pointer to previous S but as normal pointer can't be placed onto shared_ptr, I can't go backwards when I've reached the end of the linked list (I need to go to the end so that I can go backwards one S at time and release the currently last element by making the last-1 element point to nullptr.).
And doubly won't work with next = shared_ptr and previous = shared_ptr cause elements would point to each other and shared pointers couldn't release memory ever.
Or should I do the array differently or just use only normal pointers?