Poll: Do you use smart pointers?

J

Jim Langston

Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't think
that most C++ programmers use smart pointers, but I just don't know.

I don't like them because I don't trust them. I use new and delete on pure
pointers instead.

Do you use smart pointers?
 
V

Victor Bazarov

Jim said:
Someone made the statement in a newsgroup that most C++ programmers
use smart pointers. His actual phrase was "most of us" but I really
don't think that most C++ programmers use smart pointers, but I just
don't know.
I don't like them because I don't trust them. I use new and delete
on pure pointers instead.

Do you use smart pointers?

Yes.

V
 
T

Thomas Tutone

Jim said:
Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't think
that most C++ programmers use smart pointers, but I just don't know.

I don't like them because I don't trust them. I use new and delete on pure
pointers instead.

Do you use smart pointers?

Yes, from the Standard Library, from boost, and custom.

Best regards,

Tom
 
M

mlimber

Jim said:
Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't think
that most C++ programmers use smart pointers, but I just don't know.

I don't like them because I don't trust them. I use new and delete on pure
pointers instead.

Do you use smart pointers?

Whenever possible (which is most of the time), yes.

Cheers! --M
 
T

Thomas Kowalski

Do you use smart pointers?
No, because I don't trust myself to use them right. Besides I never had
a real need for them.

Regards,
Thomas Kowalski
 
K

Kai-Uwe Bux

Jim said:
Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't
think that most C++ programmers use smart pointers, but I just don't know.

I don't like them because I don't trust them.

Care to explain why you don't trust them?

I use new and delete on pure pointers instead.

Do you use smart pointers?

Yes. I made it a point to eliminate raw pointers from most of my code: I
even have a pointer_to<T,Alloc> template that just wraps a raw pointer. It
does not add much: a hook for a custom allocator (defaults to new/delete)
and a compile time check against the use of polymorphic pointers when the
underlying base type has no virtual destructor.

Another smart pointer that I use for debugging is a drop in replacement for
pointer_to<> that traces allocations and deallocations and helps in
diagnosing memory leaks and double deletion.

I use reference counted smart pointers for managing shared resources that
shall be released when the last co-owner dies.


Best

Kai-Uwe Bux
 
H

Heinz Ozwirk

Jim Langston said:
Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't
think that most C++ programmers use smart pointers, but I just don't know.

I don't like them because I don't trust them. I use new and delete on
pure pointers instead.

Then write your own implementation. And if you don't trust yourself then,
how can you trust yourself when using raw pointers?
Do you use smart pointers?

Yes.

Heinz
 
P

Phlip

Jim said:
Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't
think that most C++ programmers use smart pointers, but I just don't know.

Correct use of smart pointers is part of knowing C++. The language cannot
rely on just its raw keywords, or on its Standard Library. C++ also requires
a corpus of "sane subset" guidelines to use correctly, to write flexible
code with low risk.
I don't like them because I don't trust them. I use new and delete on
pure pointers instead.

Do your colleagues approve? Don't you ever leak?

(Note I don't claim my colleagues approve of my smart pointers, or that I
never leak!;)
 
P

Phlip

Mingming said:
Does smart pointer use too many resource?

Premature optimization is the root of all evil.

(Google for that sentence!)

Small smart pointers are the same size as pointers, and they insert opcodes
to call 'delete' into all the places where you should have coded 'delete'.
So their resource impact is minimal.
 
P

Phlip

Heinz said:
Then write your own implementation. And if you don't trust yourself then,
how can you trust yourself when using raw pointers?

Next tip: Always fold duplicated code into reusable abstractions. So put
your many 'delete's into one place and reuse it. The destructor of a smart
pointer.

Next tip: Prefer automatic and member objects over heap objects. ;-)
 
H

Howard

Phlip said:
Correct use of smart pointers is part of knowing C++. The language cannot
rely on just its raw keywords, or on its Standard Library. C++ also
requires a corpus of "sane subset" guidelines to use correctly, to write
flexible code with low risk.


Do your colleagues approve? Don't you ever leak?

(Note I don't claim my colleagues approve of my smart pointers, or that I
never leak!;)

I leak sometimes, but that's why they invented Ptr_Depends, the smart
pointer guaranteed to stop those embarassing leaks!

(Sorry, couldn't resist.)

-Howard
 
P

peter koch

Jim said:
Someone made the statement in a newsgroup that most C++ programmers use
smart pointers. His actual phrase was "most of us" but I really don't think
that most C++ programmers use smart pointers, but I just don't know.

I don't like them because I don't trust them. I use new and delete on pure
pointers instead.

That is absolute rubbish. You should trust library code which has been
tested by lots of users all over the world and distrust other code -
including your own.
Do you use smart pointers?

Whenever I can. I am working on a very old codebase right now, and
there are raw pointers here and there. I do intend to replace all raw
pointers (to an extent that I will not find a delete in the "user"-code
anywhere).
Unfortunately, theres still a long way to go.

/Peter
 
H

Howard

peter koch said:
That is absolute rubbish. You should trust library code which has been
tested by lots of users all over the world and distrust other code -
including your own.

Kind of harsh isn't it, calling someone's personal opinion "absolute
rubbish"?

I still don't use smart pointers very often, mainly because I've been using
raw pointers for many years now, and it's often easier for me to keep doing
what I've always been doing.

Also, don't the STL containers require smart pointers which are not
themselves found in the STL? It seems to me that things like std::vector
require using something like the Boost smart pointers. I forget the
reasoning exactly, but that's correct, isn't it? And many of us don't have
Boost, so what's the alternative?

As my confusion above might illustrate, many of us probably just don't know
the best way to make regular use of smart pointers, or which one(s) to use.
Perhaps we just need a little nudge in the right direction...?

-Howard
 
A

Alf P. Steinbach

* Phlip:
Next tip: Always fold duplicated code into reusable abstractions.

On my current project, working as a hired hand after some years off the
job market, the manager insists on duplicating a whole set of some forty
plus files in two mutually communicating applications, and that the
files absolutely /must/ be different (it's specifically and very very
strongly disallowed to make one file that can be used identically in
both places), using manual update to keep the files in synch, and source
control merging (twice for each near-duplicated file) as a general
solution to the problem of him working on the same files as others.

This simple device allows the manager, who also does some programming,
to create any desired amount of problems for the others, who are placed
physically at maximally separated locations in a large building, so as
to avoid any teamwork or cross-communication (60% of the project members
are now on the way out of not just the project but out of the firm).

Needless to say I won't be working there again, but amazingly they /do/
use smart pointers, and these smart pointers have a feature that allow
raw pointers to be passed around, even for smart-pointer managed
objects, namely, that the smart pointers are intrusive, keeping the
reference count in the referred object rather than in the smart pointer.
The smart pointers also have implicit conversion to and from raw
pointers, to make it even more simple & natural to use raw pointers for
managed objects. Still, this was such a great improvement over just raw
pointers that one of the original developers commented at lunch that "I
now think we should have used /only/ smart pointers, everywhere".

Uh, have I said enough?

Think so, yes...
 
N

Noah Roberts

Alf said:
* Phlip:

On my current project, working as a hired hand after some years off the
job market, the manager insists on duplicating a whole set of some forty
plus files in two mutually communicating applications, and that the
files absolutely /must/ be different (it's specifically and very very
strongly disallowed to make one file that can be used identically in
both places), using manual update to keep the files in synch, and source
control merging (twice for each near-duplicated file) as a general
solution to the problem of him working on the same files as others.

Yes, insainity.

The problem I have at my current position is that 'management' is
scared to use anything that requires they learn something new. The new
manager is better than the last who decided everything should be
written in-house but is still queezy about learning new things.
Problem is that most everything is new to them...I am the resident
expert on C++ and the STL and that is just sad. Hell, when I first got
there I had to justify every use of std::string - it should be the
other way, justify when you don't.

Frankly I don't understand anyone that doesn't constantly teach
themselves new techniques and _want_ to learn everything they can. I'm
always getting books, reading material on the web, experimenting at
home...these guys do only what they have to at work and do no extra
learning at home. It rather tripps me out.
 
T

Thomas Tutone

Howard said:
Kind of harsh isn't it, calling someone's personal opinion "absolute
rubbish"?

I still don't use smart pointers very often, mainly because I've been using
raw pointers for many years now, and it's often easier for me to keep doing
what I've always been doing.

Also, don't the STL containers require smart pointers which are not
themselves found in the STL?

The standard containers do not require smart pointers, but using smart
pointers with the standard containers makes life much easier than using
the standard containers with raw pointers.
It seems to me that things like std::vector
require using something like the Boost smart pointers.

Not true. Raw pointers work fine, but are a sure recipe for leaks.
I forget the
reasoning exactly, but that's correct, isn't it?
No.

And many of us don't have
Boost, so what's the alternative?

The FAQ includes a perfectly adequate intrusive smart pointer that I
have used for years:

http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.22

And why can't you use boost? Does your employer not permit it?
As my confusion above might illustrate, many of us probably just don't know
the best way to make regular use of smart pointers, or which one(s) to use.
Perhaps we just need a little nudge in the right direction...?

The FAQ will start you in the right direction. Jossuttis' The C++
Standard Library has some good examples of using a non-intrusive smart
pointer (using code supplied in the book) that works well with standard
containers.

Best regards,

Tom
 

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

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top