473,399 Members | 3,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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
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
Jul 22 '05 #1
9 2109
"christopher diggins" <cd******@videotron.ca> wrote in message
news:aM********************@weber.videotron.net...
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
Jul 22 '05 #2

christopher diggins wrote:
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!)
Jul 22 '05 #3

"christopher diggins" <cd******@videotron.ca> wrote in message
news:aM********************@weber.videotron.net...
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
Jul 22 '05 #4

"christopher diggins" <cd******@videotron.ca> skrev i en meddelelse
news:aM********************@weber.videotron.net...
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
Jul 22 '05 #5
----- Original Message -----
From: "christopher diggins" <cd******@videotron.ca>
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
Jul 22 '05 #6

"christopher diggins" <cd******@videotron.ca> wrote in message
news:aM********************@weber.videotron.net...
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
Jul 22 '05 #7
"christopher diggins" <cd******@videotron.ca> wrote in message news:<aM********************@weber.videotron.net>. ..
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.
Jul 22 '05 #8
On Thu, 11 Nov 2004 19:48:59 -0500, "christopher diggins"
<cd******@videotron.ca> wrote:
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<>.
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.

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #9
Thank you everyone, the contributions on the smart pointers have been
consistently excellent, and extremely insightful!

Christopher Diggins
http://www.cdiggins.com
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
11
by: Rajeev | last post by:
Dear friends I am conducting a survey on Relational Database usage and would like your help. The study is part of my MBA Dissertation. Could you kindly spare 5 minutes to take part in this...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
92
by: Jim Langston | last post by:
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,...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
4
by: Alia Khouri | last post by:
Can we open up the discussion here about how to improve setuptools which has become the de facto standard for distributing / installing python software. I've been playing around with ruby's gems...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.