Survey: Frequency of usage of smart pointers

  • Thread starter christopher diggins
  • Start date
C

christopher diggins

I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's code
you have come across would be appreciated. I am also trying to identify the
likelihood nad frequency of scenarios where smart pointer solutions would
not be appropriate, i.e. for some reason such as performance or complexity.
Oh, and if you feel like mentioning your favourite smart pointer libraries
that would be appreciated as well!

Thank you very much!

Christopher Diggins
http://www.cdiggins.com
 
M

Mike Wahler

christopher diggins said:
I would like to survey how widespread the usage of smart pointers in C++
code is today.

I've never used them (except for experimentation in the interest
of learning). But then again, I don't write huge amounts of C++
code either.
Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's code
you have come across would be appreciated.

So far, all my needs for dynamically allocated objects have been
served by the standard containers.
I am also trying to identify the
likelihood nad frequency of scenarios where smart pointer solutions would
not be appropriate, i.e. for some reason such as performance or
complexity.

I really don't think such a statistic would be meaningful unless
it were broken down into contextual categories. Performance and
(high vs low) complexity will fall under different levels of priority
for various solutions. Almost always a high priority for me (in
any language), is clarity of expression (i.e. maintainability).
IMO the standard containers meet this criterion quite well. IOW,
not only don't I use smart pointers, I rarely use 'new', 'delete'
or their array forms.
Oh, and if you feel like mentioning your favourite smart pointer libraries
that would be appreciated as well!

Sorry, don't have one.

-Mike
 
G

Gianni Mariani

christopher said:
I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's code
you have come across would be appreciated.

I have used some form of smart pointer since '2000 in 3 separate large
projects. They have all been the "intrusive" type (not boost::shared_ptr).

In the newer projects they are used very liberally. Virtually any
dynamically allocated object is managed with reference counted smart
pointers and no API's use any raw pointers for any classes that are
designed to be dynamically allocated.

YMMV.

I am also trying to identify the
likelihood nad frequency of scenarios where smart pointer solutions would
not be appropriate, i.e. for some reason such as performance or complexity.

In multithreaded applications, reference counting needs to be done using
a significantly more expensive (up to 100 times more expensive depending
on HW) "atomic" increment/decrement, so I use a different "smart"
pointer when passing pointers in cases when I know that the object will
not be deleted during the life of the function call, hence eliminating 2
(or more) times the overhead of incrementing and decrementing the
reference count. Austria C++ smart pointers have "Ptr<>",
"PtrDelegate<>" and "PtrView<>" smart pointer variants that (if used
correctly) can eliminate much of the wasted increment/decrement of
reference counts.
Oh, and if you feel like mentioning your favourite smart pointer libraries
that would be appreciated as well!

Austria C++. (as the author you can't expect me to write anything else!)
 
J

John Harrison

christopher diggins said:
I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's
code you have come across would be appreciated. I am also trying to
identify the likelihood nad frequency of scenarios where smart pointer
solutions would not be appropriate, i.e. for some reason such as
performance or complexity. Oh, and if you feel like mentioning your
favourite smart pointer libraries that would be appreciated as well!

I would say that they get used in most new projects I work on. They aren't
very good at interfacing with legacy code that uses raw pointers however.
That would be my only reason not to use them.

Efficiency has never ever been a concern, and smart pointers reduce
complexity dramatically, so I can't see either of those objections being
applicable very often.

john
 
P

Peter Koch Larsen

christopher diggins said:
I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's
code you have come across would be appreciated. I am also trying to
identify the likelihood nad frequency of scenarios where smart pointer
solutions would not be appropriate, i.e. for some reason such as
performance or complexity. Oh, and if you feel like mentioning your
favourite smart pointer libraries that would be appreciated as well!

Thank you very much!

Christopher Diggins
http://www.cdiggins.com

I've used smart pointers since i wrote my first serious C++-project in 1996.
Since then I haven't called delete outside that class (except for testcode,
perhaps).
I should start boosts smartpointer library, but i've come to like my own
class and laziness has prevented the refactoring.


/Peter
 
J

Jeff Flinn

----- Original Message -----
From: "christopher diggins" <[email protected]>
Newsgroups: comp.lang.c++
Sent: Thursday, November 11, 2004 7:48 PM
Subject: Survey: Frequency of usage of smart pointers

I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's code
you have come across would be appreciated. I am also trying to identify
the

They are the norm in all of my projects. The only place I seem to ever write
a
'new' is in the argument list to the smart pointer constructor. I can't
remember
the last time I typed d-e-l-e-t-e. The older applications that I've
inherited
were in C++ written by C programmers. Most of the bugs I found were
corrected by
replacing manual dynamic allocation with smart pointers and/or std
containers
likelihood nad frequency of scenarios where smart pointer solutions would
not be appropriate, i.e. for some reason such as performance or
complexity.

Certainly the more complex the application there's even more reason to use
smart
pointers versus manual memory management. The Glow Code profiler has never
pointed to shared_ptr as a performance bottleneck.

Aah, I do remember writing delete now. It is called in a class derived from
MFC's CTreeCtrl. It manages the lifetimes of it's node data, which holds a
unsigned long casted polymorphic pointer. I've never been comfortable with
this
arrangement and eventually plan to re-design an entirely new tree view
class, or
to have the existing class have a std::set< shared_ptr<TreeItm> > and store
a
key in the node data struct.
Oh, and if you feel like mentioning your favourite smart pointer libraries
that would be appreciated as well!

1) boost::shared_ptr
2) boost::scoped_ptr
2) std::auto_ptr

Jeff F
 
A

andre dajd

christopher diggins said:
I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's
code you have come across would be appreciated. I am also trying to
identify the likelihood nad frequency of scenarios where smart pointer
solutions would not be appropriate, i.e. for some reason such as
performance or complexity. Oh, and if you feel like mentioning your
favourite smart pointer libraries that would be appreciated as well!

1) Fully fledged thread safe smart pointers are must in cross architecture
systems, eg Java/C++
2) Shared pointers are useful in heavy mathematical applications with
optimized low level memory access. Main advantages are math algorithms code
simplification and facilitation of various fine-grained implementation
extension (as opposed to interface extension) policies that is when you
provide a placeholder for a hanlde that must be fill elsewhere:

template<typename T>
struct Handle
{
smartPtr<T> ptr;
};

template<typename t1, typename t2>
class User
: protected Handle<t1>, protected Handle<t2>
{

};

Combination of both requirements is standard eg for any modern banking
(trading/risk management) system.

std::auto_ptr is useless in most cases; boost stuff is most useful.

d
 
B

Branimir Maksimovic

christopher diggins said:
I would like to survey how widespread the usage of smart pointers in C++
code is today.

Well, member variables, local variables, function parameters,
function return values.

Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's code
you have come across would be appreciated.

If you want to hook C programmer to start with C++ show him/her smart
pointers and exceptions.:)

I am also trying to identify the
likelihood nad frequency of scenarios where smart pointer solutions would
not be appropriate, i.e. for some reason such as performance or complexity.

Smart pointers are just subroutines, that code must be written
with or without them. Try without and see what I mean :)
Oh, and if you feel like mentioning your favourite smart pointer libraries
that would be appreciated as well!

std::auto_ptr

Greetings, Bane.
 
B

Bob Hairgrove

I would like to survey how widespread the usage of smart pointers in C++
code is today. Any anecdotal experience about the frequency of usage of
smart pointer for dynamic allocation in your own code or other people's code
you have come across would be appreciated.

How about "almost all the time"?

I couldn't begin to count the lines of code I had to refactor which
was written by a colleague who allocated character arrays with new[]
and invariably used plain delete instead of delete[] on the pointer,
simply forgot to delete at all, or his code was not exception safe
(meaning that delete was skipped if an exception was thrown). Believe
it or not, there are many programmers working in the real world who
have never heard of smart pointers.

Of course, I used std::string as often as possible, but there were
times when a non-const buffer was required. Now I would probably use
std::vector<char>, but this was before the standard required memory
allocation of vector to be contiguous (or before I knew about it<g>).

So I used either an array allocated on the stack if the size was known
in advance; if not, we used a modified form of the "grin pointer"
written by Allan Griffiths (see:
http://www.octopull.demon.co.uk/arglib/). Essentially, I just added a
bool argument indicating whether plain delete or array delete should
be used, and since we only needed automatic deletion and didn't need
to copy them, I stripped out that part of the code.

The grin pointer was also used for implementing classes with the
"pimpl idiom" in cases where pointers to more than one implementation
class were kept as members and these needed to be allocated in the
constructor's initialization list. Grin works well on incomplete types
which are merely forward declared in the header and only included
later in the .cpp file.
I am also trying to identify the
likelihood nad frequency of scenarios where smart pointer solutions would
not be appropriate, i.e. for some reason such as performance or complexity.

Some smart pointers are inappropriate for use in containers, e.g.
std::auto_ptr said:
Oh, and if you feel like mentioning your favourite smart pointer libraries
that would be appreciated as well!

I think the Boost library pretty much covers it all. But check out
Allan Griffith's site as well.
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top