Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

shared_ptr and unique_ptr related question

50 views
Skip to first unread message

somenath

unread,
Feb 21, 2014, 8:28:19 PM2/21/14
to
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.

--
Somenath

Paavo Helde

unread,
Feb 22, 2014, 4:09:00 AM2/22/14
to
somenath <somen...@gmail.com> wrote in
news:1b336c7c-f523-4bd8...@googlegroups.com:

> class ListItem {
> public:
> ListItem( T Val );
> shared_ptr <ListItem > Next;
> T Data;
>
> };
[...]
> 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?

Shared pointers are useful when you need to refer to the same object from
different places and you don't have good or natural control over the
lifetime of the object otherwise. So, using them in an implementation
*inside* of a list of items is pretty pointless, as you have noticed
yourself.

A need to refer to the same object from different places in this way
arises only if these are so-called entity objects (maintaining a changing
state) or they are too expensive or too large to copy casually. In both
cases the copy ctor and assignment operator should be absent or at least
not readily accessible, which is in direct violation with your code where
T objects are passed and held by value and copied all over the place.


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

Our codebase uses them extensively, but the code is proprietary, sorry.
Mostly it is about maintaining data items in a script language
interpreter.

But take any look of some Windows C++ code using COM (or whatever it is
called this year). The COM objects are reference counted and disposed
when the last reference drops. There is a _com_ptr_t class template
which is used for defining smartpointers, starting from IUnknownPtr and
up to things like MSHTML::IHTMLDocument2Ptr. Any C++ code should use
those smartpointers for accessing COM interfaces (it is possible to do it
the hard C way as well, but this is error-prone, especially when mixed
with C++ exceptions).

Cheers
Paavo

James Kanze

unread,
Feb 23, 2014, 9:10:31 AM2/23/14
to
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.)

--
James
0 new messages