Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > I don't get why smart pointers are useful.

Reply
Thread Tools

I don't get why smart pointers are useful.

 
 
Simon L
Guest
Posts: n/a
 
      04-14-2008

Pointer mismanagement is a big cause of bugs in software that I work
on. I decided to invest a little time into
having a look at auto_ptr and other smart pointers but I don't see how
they'll save me making the same mistakes.


A typical use and source of buggy pointers is in a collection of
objects. My containers of choice, eg stl::vector
don't like auto_ptr so I've had to use some MFC template classes.

In this test app I don't see how my pointers are suddenly safe or
smart. Yes they transfer ownership, yes they
auto delete but what I was hoping (expecting) was that manipulation of
old (ie ownership transferred or cleaned)
auto_ptrs would be handled.

Use of the pointer selection -> on my old auto_ptr blows up my app.,
gaining me very little as far as I can see.

Is it that my demo. app. is too trivial to see the benefits? If I have
to wrap all use of my object in if( something !=null) then I've
gained nothing.

Thanks


class cNew
{
public:
cNew(int i, LPCTSTR pc)
{
iNew = i;
strcpy(cText , pc);
}
char* GetCNew(){return cNew;}
int iNew;
char cText[256];
};

CList<auto_ptr<sNew> , auto_ptr<sNew>& > m_Arr;

void CSTLTestDlg::OnBnClickedOk()
{

auto_ptr<sNew> aNew ( new cNew(1, CString("Hello World")));

AfxMessageBox( aNew->GetCNew() , MB_OKCANCEL );

m_Arr.AddTail( aNew );
//ownership of cNew passes to an auto_ptr in my List

AfxMessageBox( m_Arr.GetHead()->GetCNew() , MB_YESNO );
//seems to contain valid data

char* pcData = aNew->GetCNew();
//pcData is non null eg random memory

int iData = aNew->iNew;
//exception

m_Arr.RemoveAll();

OnOK();

}
 
Reply With Quote
 
 
 
 
Vladislav.Lazarenko@gmail.com
Guest
Posts: n/a
 
      04-14-2008
On Apr 14, 6:38 pm, Simon L <silang...@hotmail.com> wrote:
> Pointer mismanagement is a big cause of bugs in software that I work
> on. I decided to invest a little time into
> having a look at auto_ptr and other smart pointers but I don't see how
> they'll save me making the same mistakes.
>


Well designed smart pointers library will not allow you to make a
mistake. It is not a good idea to use std::auto_ptr template with
vector or any other collection that performs object copying.
std::auto_ptr will not manage this situation for you. Never.

Please take a loot at the following:

* Pointer Container - http://www.boost.org/doc/libs/1_35_0...container.html
* Boost Shared Pointer - http://www.boost.org/doc/libs/1_35_0...shared_ptr.htm

Hope you will find these links helpful.

- Vlady
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      04-14-2008
wrote:
> On Apr 14, 6:38 pm, Simon L <silang...@hotmail.com> wrote:
>> Pointer mismanagement is a big cause of bugs in software that I work
>> on. I decided to invest a little time into
>> having a look at auto_ptr and other smart pointers but I don't see how
>> they'll save me making the same mistakes.
>>

>
> Well designed smart pointers library will not allow you to make a
> mistake. It is not a good idea to use std::auto_ptr template with
> vector or any other collection that performs object copying.
> std::auto_ptr will not manage this situation for you. Never.


Well, std::auto_ptr violates copy and assignment semantics. It therefor
CAN'T be used in a std::vector. You can attempt to, sure, but it will
not work and that's why.
 
Reply With Quote
 
sk_usenet
Guest
Posts: n/a
 
      04-14-2008

"Noah Roberts" <> wrote in message
>> On Apr 14, 6:38 pm, Simon L <silang...@hotmail.com> wrote:
>>> Pointer mismanagement is a big cause of bugs in software that I work
>>> on. I decided to invest a little time into
>>> having a look at auto_ptr and other smart pointers but I don't see how
>>> they'll save me making the same mistakes.
>>>

>>
>> Well designed smart pointers library will not allow you to make a
>> mistake. It is not a good idea to use std::auto_ptr template with
>> vector or any other collection that performs object copying.
>> std::auto_ptr will not manage this situation for you. Never.

>
> Well, std::auto_ptr violates copy and assignment semantics. It therefor
> CAN'T be used in a std::vector. You can attempt to, sure, but it will not
> work and that's why.


Yes, it is a case of undefined behavior.

--
http://techytalk.googlepages.com


 
Reply With Quote
 
Vladislav.Lazarenko@gmail.com
Guest
Posts: n/a
 
      04-14-2008
On Apr 14, 7:53 pm, Noah Roberts <u...@example.net> wrote:
> Vladislav.Lazare...@gmail.com wrote:
> > On Apr 14, 6:38 pm, Simon L <silang...@hotmail.com> wrote:
> >> Pointer mismanagement is a big cause of bugs in software that I work
> >> on. I decided to invest a little time into
> >> having a look at auto_ptr and other smart pointers but I don't see how
> >> they'll save me making the same mistakes.

>
> > Well designed smart pointers library will not allow you to make a
> > mistake. It is not a good idea to use std::auto_ptr template with
> > vector or any other collection that performs object copying.
> > std::auto_ptr will not manage this situation for you. Never.

>
> Well, std::auto_ptr violates copy and assignment semantics. It therefor
> CAN'T be used in a std::vector. You can attempt to, sure, but it will
> not work and that's why.


That's exactly what I am talking about. I use ptr_container for Boost
for these purposes or list of shared pointer if they should really be
shared
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-14-2008
Simon L wrote:
> Use of the pointer selection -> on my old auto_ptr blows up my app.,
> gaining me very little as far as I can see.


Thus auto_ptr is the wrong tool in your case. You clearly want a
*shared* object, not a transferred one (which is what auto_ptr does).

The standard C++ libraries do not (yet) provide smart pointers for
shared objects, but eg. the boost library provides very good ones. You
should try them.

(And btw, trying to use auto_ptr with std::vector shouldn't even
compile. If your compiler compiled it anyways, it's broken.)
 
Reply With Quote
 
Noah Roberts
Guest
Posts: n/a
 
      04-16-2008
Juha Nieminen wrote:

> (And btw, trying to use auto_ptr with std::vector shouldn't even
> compile. If your compiler compiled it anyways, it's broken.)


Why shouldn't it compile? Unless I'm mistaken, using std::auto_ptr in a
container is a conceptual violation, not a syntatic one. How would the
compiler even know? It would have to be using concepts or traits of
some sort and a bunch of metaprogramming. The standard library doesn't
do that yet since neither is part of the standard yet.
 
Reply With Quote
 
Juha Nieminen
Guest
Posts: n/a
 
      04-16-2008
Noah Roberts wrote:
> Juha Nieminen wrote:
>
>> (And btw, trying to use auto_ptr with std::vector shouldn't even
>> compile. If your compiler compiled it anyways, it's broken.)

>
> Why shouldn't it compile? Unless I'm mistaken, using std::auto_ptr in a
> container is a conceptual violation, not a syntatic one. How would the
> compiler even know? It would have to be using concepts or traits of
> some sort and a bunch of metaprogramming. The standard library doesn't
> do that yet since neither is part of the standard yet.


Normally copy constructors take a const reference to the object to be
copied, while auto_ptr takes a non-const reference. If you try to copy a
const object like this, it will fail.

Of course it may be possible that some std::vector implementation
never uses 'const' anywhere, in which case it may happily compile
without error.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Smart Pointers: Is there something similar to smart pointers in C? MotoK C Programming 59 09-15-2006 07:03 PM
Smart pointers and member function pointers n2xssvv g02gfr12930 C++ 3 11-27-2005 10:51 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57