Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
    
Use normal pointers. Alternatively, why not std::list? –  David K 2 days ago

2 Answers 2

To answer your first question, objects of type

std::shared_ptr<T>

share their ownership and only delete the managed object when the last shared_ptr is destroyed. You can, however, make the shared_ptr release its managed object by calling

std::shared_ptr<T>::reset

see std::shared_ptr::reset

Additionally, a call to this function has the same side effects as if shared_ptr's destructor was called before its value changed (including the deletion of the managed object if this shared_ptr was unique).

As a building block of linked lists, don't use shared_ptr, use normal pointers. To quote "The C++ Programming Language" by Bjarne Stroustrup,

Use shared_ptr only if you actually need shared ownership.

share|improve this answer
    
I changed the shared pointers to normal ones. But to think about the original dilemma: When I set the shared_ptr @ array[i] to nullptr, doesn't that make the element, that shared_ptr was pointing to, disappear? And when that element disappears, so does the shared_ptr to the next element... and when that doesn't exist anymore, nothing is pointing the next element. So, will the shared pointers release memory like domino effect in this case? –  Masza 2 days ago
    
"A shared_ptr that does not own any pointer is called an empty shared_ptr. A shared_ptr that points to no object is called a null shared_ptr and shall not be dereferenced. Notice though that an empty shared_ptr is not necessarily a null shared_ptr, and a null shared_ptr is not necessarily an empty shared_ptr." (From cplusplus.com) Setting a pointer to nullptr does not "delete" the object it pointed to, it simply makes the pointer not point to anything. –  jensa 2 days ago
    
What happens to the object created with make_shared when shared_ptr that pointed to it is set to nullptr? –  Masza 2 days ago
    
Actually, setting a shared_ptr to nullptr actually calls reset(), so you can choose which one you want (calling reset or setting to nullptr). The object that is pointed to is destroyed. –  jensa yesterday

It seems that

array[i] = nullptr;

does release all the memory as array[i] is shared_ptr and setting it to nullptr "deletes" (as jensa mentioned, calls reset()) the object the shared_ptr was pointing to, deleting also the shared_ptr (of the object) to the next object and because of that the next object is deleted also... and this goes on till there's no shared_ptrs/objects left in the linked list at array[i].

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.