shared_ptr and unique_ptr related question

S

somenath

To get some understanding of shared_ptr and unique_ptr I wrote the following naive implementation of linked list.

#include<memory>
using namespace std;

template <class T>
class List {
private:
class ListItem {
public:
ListItem( T Val );
shared_ptr <ListItem > Next;
T Data;

};

shared_ptr< ListItem >Head;

public:
int CreateNode();
List() {
Head.reset();
}
void PushBack(T Val);
void Dump();
};


template<class T>
List<T>::ListItem::ListItem( T Val):Data(Val) {
Next.reset();


}
template<class T>
void List<T>::pushBack( T val)
{
unique_ptr<ListItem > NewItem (new ListItem(val));
if (!Head ) {
Head = move(NewItem);
}
else {
shared_ptr<ListItem> Curr(Head);
shared_ptr<ListItem> Prev(Head);
while( (Curr) ) {
Prev = Curr;
Curr = Curr->Next;
}
Prev->Next = move(NewItem);
}
}

template<class T>
void List<T>::Dump() {
shared_ptr<ListItem > Curr(Head);
while ( Curr) {
cout<<"Val = "<<Curr->Data<<endl;;
Curr = Curr->Next;
}
}

But I am not very clear of the correct uses of shared_ptr and auto_ptr yet.Could you please comment on the uses of smart pointers in the context of my linked list code. According to my understanding where I do not need to assign smart pointers to other one I use unique_ptr . Is this understanding correct?

Also I am not able to get convinced the benefit of shared_ptr and unique_ptr in the context of my code. I could have used even raw pointers to implement the same without losing much benefit . Is it not the case here?

I can use the above code as

int main(int argc,char *argv[] )
{
unique_ptr< List <int> > ilst (new List<int>());

ilst->PushBack(5);
ilst->PushBack(15);
ilst->PushBack(25);
ilst->Dump();

return 0;
}

Is the benefit of using smart pointers is, ilst need to be freed manually?

Please point me to some real code where smart pointers has been heavily used.
 
J

James Kanze

To get some understanding of shared_ptr and unique_ptr I wrote
the following naive implementation of linked list.
#include<memory>
using namespace std;
template <class T>
class List {
private:
class ListItem {
public:
ListItem( T Val );
shared_ptr <ListItem > Next;
T Data;
};
shared_ptr< ListItem >Head;
public:
int CreateNode();
List() {
Head.reset();
}
void PushBack(T Val);
void Dump();
};
template<class T>
List<T>::ListItem::ListItem( T Val):Data(Val) {
Next.reset();
}
template<class T>
void List<T>::pushBack( T val)
{
unique_ptr<ListItem > NewItem (new ListItem(val));
if (!Head ) {
Head = move(NewItem);
}
else {
shared_ptr<ListItem> Curr(Head);
shared_ptr<ListItem> Prev(Head);
while( (Curr) ) {
Prev = Curr;
Curr = Curr->Next;
}
Prev->Next = move(NewItem);
}
}
template<class T>
void List<T>::Dump() {
shared_ptr<ListItem > Curr(Head);
while ( Curr) {
cout<<"Val = "<<Curr->Data<<endl;;
Curr = Curr->Next;
}
}
But I am not very clear of the correct uses of shared_ptr and
auto_ptr yet.

Then don't use them. They're useful for some special cases; if
your use corresponds to one of those special cases, fine, but if
not, don't use them. Using them for memory contained in a
larger entity, like a list, is an anti-pattern.

Take a look at the implementations of the standard containers.
None of them use any classical smart pointers. (Most do use
private base classes to ensure correct destruction of partially
constructed objects. This is another technique, perhaps more
applicable to container classes in general.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top