 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
christopher diggins Guest
|
Posted: Fri Nov 12, 2004 12:08 pm Post subject: smart pointer usage frequency |
|
|
I am curious about how often C++ programmers today use smart pointers
instead of raw pointers. I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries (i.e.
performance concerns, complexity, laziness, unawareness). Any kind of
relevant anecdotal experiences, or observations would be appreciated.
Thanks a lot!
Christopher Diggins
http://www.heron-language.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Malte Clasen Guest
|
Posted: Fri Nov 12, 2004 3:44 pm Post subject: Re: smart pointer usage frequency |
|
|
christopher diggins wrote:
| Quote: | I am curious about how often C++ programmers today use smart pointers
instead of raw pointers. I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries (i.e.
performance concerns, complexity, laziness, unawareness). Any kind of
relevant anecdotal experiences, or observations would be appreciated.
|
My recent projects (scientific visualization, real-time rendering) use
the boost smart pointers exclusively. There's no noticable performance
overhead, but that might be caused by the GPU bottleneck that's inherent
to this field of application.
Previously I was using a mixture of std::auto_ptr and raw pointers. It
was better than raw pointers alone, but the combination resulted in
frequent confusion. One advantage of that solution was the very clear
concept of ownership and the explicit ownership transfer by passing
auto_ptr as a function parameter. But the convenience of not having to
differentiate between two kinds of pointers and the ability to store
every pointer in stl containers outweighted this detail, so I switched
to the boost pointers.
Malte
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gerhard Wesp Guest
|
Posted: Fri Nov 12, 2004 3:45 pm Post subject: Re: smart pointer usage frequency |
|
|
christopher diggins <cdiggins (AT) videotron (DOT) ca> wrote:
| Quote: | I am curious about how often C++ programmers today use smart pointers
instead of raw pointers. I am also curious as to what kinds of obstacles
|
At our company, almost exclusively smart pointers.
Cheers
-Gerhard
--
Gerhard Wesp o o Tel.: +41 (0) 43 5347636
Bachtobelstrasse 56 | http://www.cosy.sbg.ac.at/~gwesp/
CH-8045 Zuerich _/ See homepage for email address!
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Fri Nov 12, 2004 7:31 pm Post subject: Re: smart pointer usage frequency |
|
|
christopher diggins wrote:
| Quote: | I am curious about how often C++ programmers today use smart pointers
instead of raw pointers. I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries (i.e.
performance concerns, complexity, laziness, unawareness). Any kind of
relevant anecdotal experiences, or observations would be appreciated.
|
I vaguely recall things that have '*' in their type names.
But, that's because I get to write all the code, and have few
external dependencies.
I found intrusive reference counting to do best (performance)
when I need pointers. Otherwise, I stick to references for
indirection.
Among other things, I avoid the cases where deep copies
are "required" to alleviate pointer ownership confusions.
Can't say I miss the old raw pointer much.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Roland Pibinger Guest
|
Posted: Sat Nov 13, 2004 2:52 am Post subject: Re: smart pointer usage frequency |
|
|
On 12 Nov 2004 07:08:19 -0500, "christopher diggins"
<cdiggins (AT) videotron (DOT) ca> wrote:
| Quote: | I am curious about how often C++ programmers today use smart pointers
instead of raw pointers.
|
Smart pointers normally cannot supplant real pointers. There exist
hardly any real-world uses cases for smart pointers. Some of them, eg.
shared_ptr, are probably mostly used by developers trained in Java
(C#, or other GC languages) who want to program in C++ without looking
deeper into core C++ issues like resource management.
| Quote: | I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries (i.e.
performance concerns, complexity, laziness, unawareness).
|
The main obstacles are the smart pointers, luckily :-)
| Quote: | Any kind of
relevant anecdotal experiences, or observations would be appreciated.
|
The current smart pointer fad is at least the 4th in C++ history and
it will go away like the others before.
Best regards,
Roland Pibinger
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Paavo Helde Guest
|
Posted: Sat Nov 13, 2004 3:26 am Post subject: Re: smart pointer usage frequency |
|
|
"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote in
news:wkMkd.10313$Kd1.45106 (AT) weber (DOT) videotron.net:
| Quote: | I am curious about how often C++ programmers today use smart pointers
instead of raw pointers. I am also curious as to what kinds of
obstacles still exist with regards to full adoption of smart pointer
libraries (i.e. performance concerns, complexity, laziness,
unawareness). Any kind of relevant anecdotal experiences, or
observations would be appreciated.
|
I and my colleagues use smartpointers extensively in our projects (own-
defined, intrusive). Could not imagine to manage without them.
OTOH, I have to use some code which uses a lot of bare pointers (probably
because only these can be stored in MFC's ObArray (?)). As a result, my
debugger quite often stops on access violation exceptions occurring in
the Windows SDK IsBadReadPtr()/IsBadWritePtr() calls - it seems they are
not able to verify the validity of the pointers otherwise. And that's
probably the main reason they cannot deliver a Release version of their
libraries, as in the Release version the memory initialization and layout
are different.
FYI: IsBadReadPtr()/IsBadWritePtr() are Windows SDK functions which
essentially check if the memory area is readable/writable, relaying on
the hardware faults. Needless to say, this is quite unreliable method of
checking pointer validity.
The only issue with smartpointers is that we cannot create cyclic data
structures (no weak pointer implemented), but this might actually have a
positive impact as forcing to make the design non-cyclic and therefore
more simple and understandable.
About performance concerns: if some codepiece obtains a copy of
smartpointer then it will most probably dereference it and access the
pointed object's memory, so that the refcount increment inside the object
is probably neglectable as the object must be loaded in the processor
cache anyway. I imagine the things could be different in case of non-
intrusive refcounting where the refcount might sit in a totally unrelated
memory region and cause a lot of cache misses.
Cheers
Paavo
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
christopher diggins Guest
|
Posted: Sun Nov 14, 2004 10:43 am Post subject: Re: smart pointer usage frequency |
|
|
"Roland Pibinger" <rpbg123 (AT) yahoo (DOT) com> wrote
| Quote: | On 12 Nov 2004 07:08:19 -0500, "christopher diggins"
[email]cdiggins (AT) videotron (DOT) ca[/email]> wrote:
I am curious about how often C++ programmers today use smart pointers
instead of raw pointers.
Smart pointers normally cannot supplant real pointers. There exist
hardly any real-world uses cases for smart pointers. Some of them, eg.
shared_ptr, are probably mostly used by developers trained in Java
(C#, or other GC languages) who want to program in C++ without looking
deeper into core C++ issues like resource management.
I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries (i.e.
performance concerns, complexity, laziness, unawareness).
The main obstacles are the smart pointers, luckily :-)
Any kind of
relevant anecdotal experiences, or observations would be appreciated.
The current smart pointer fad is at least the 4th in C++ history and
it will go away like the others before.
Best regards,
Roland Pibinger
|
Would you mind explaining your position further? For instance why do you
view smart pointers as a passing fad? How are smart pointers an obstacle to
adoption of a smart pointer?
Christoher Diggins
http://www.cdiggins.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Darryl Green Guest
|
Posted: Sun Nov 14, 2004 4:55 pm Post subject: Re: smart pointer usage frequency |
|
|
[email]rpbg123 (AT) yahoo (DOT) com[/email] (Roland Pibinger) wrote in message news:<41950a25.374054 (AT) news (DOT) utanet.at>...
| Quote: | Smart pointers normally cannot supplant real pointers. There exist
hardly any real-world uses cases for smart pointers. Some of them, eg.
shared_ptr, are probably mostly used by developers trained in Java
(C#, or other GC languages) who want to program in C++ without looking
deeper into core C++ issues like resource management.
|
Huh? How do you conclude that a pointer that directly supports RAII
approaches to resource management is used by programmers "trained" in
other languages? In my experience, those programmers who are familiar
with C++ and are actually concerned about resource management (read
those who have some vague concept at least of a program that runs
"forever" on a platform that doesn't have "infinite" resources - that
excludes a vast majority of Java programmers in my experience) are
those who use smart pointers by default and optimise (ie. use custom
resource management) where necessary.
| Quote: | I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries (i.e.
performance concerns, complexity, laziness, unawareness).
The main obstacles are the smart pointers, luckily
|
In the sense that there remains a problem of proliferation of
ideosyncratic incompatible polices on the one hand, and trying to
manage everything with boost::shared_ptr (or tr1 std::shared_ptr) on
the other? What exactly is the benefit you are suggesting comes from
using an unmanaged pointer?
| Quote: | The current smart pointer fad is at least the 4th in C++ history and
it will go away like the others before.
|
Do you have anything concrete to offer to support this? I don't want
to waste space pointing out the reasons (which you seem to be aware
of) why resource management is a critical issue in C++ when I can only
assume you are aware of methods of addressing this without the use of
smart pointers. I'd be very interested to know what those techniques
are.
Oh - and so far as Christopher's original quesation goes - the answer,
for me at least, is all the time, except when implementing custom
resource management.
Regards
Darryl Green.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Sun Nov 14, 2004 5:00 pm Post subject: Re: smart pointer usage frequency |
|
|
Roland Pibinger wrote:
| Quote: | Smart pointers normally cannot supplant real pointers. There exist
hardly any real-world uses cases for smart pointers. Some of them, eg.
shared_ptr, are probably mostly used by developers trained in Java
(C#, or other GC languages) who want to program in C++ without looking
deeper into core C++ issues like resource management.
|
Quite a massive generalization. I've been using C++ since cfront 1.x,
and I am having a hard time accepting your statement. It may help if
you give a rationale along with this interesting opinion.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Roland Pibinger Guest
|
Posted: Mon Nov 15, 2004 11:46 am Post subject: Re: smart pointer usage frequency |
|
|
On 14 Nov 2004 05:43:36 -0500, "christopher diggins"
<cdiggins (AT) videotron (DOT) ca> wrote:
| Quote: | Would you mind explaining your position further? For instance why do you
view smart pointers as a passing fad? How are smart pointers an obstacle to
adoption of a smart pointer?
|
It is difficult to exactly define 'smart pointer' because any class
that overloads operator* could be seen as smart pointer.
In essence, a smart pointer implements a pointer (= iterator)
interface _and_ does something else (has side effects).
The problem with (some, most) smart pointer approaches is:
- the s.p. is not a pointer (iterator) any more, e.g. different
shared_ptrs cannot point to the same object
- the side effects of a s.p. (e.g. resource handling) often are
unrelated to the pointer (iterator) interface. Different tasks are
lumped together into one class (template). This is a conceptual flaw.
The s.p. waves in the last 15 or so years happend around the following
incidents:
- Daniel R. Edelson published "Smart Pointers: They're Smart, but
They're Not Pointers",
http://www-sor.inria.fr/publi/SPC++_usenixC++92.html
- Scott Meyers analyzed smart pointers in his book and in several
articles, http://www.aristeia.com/publications_frames.html
- auto_ptr (now deprecated) was introduced into the C++ Standard and
acclaimed,
http://www.gotw.ca/publications/using_auto_ptr_effectively.htm
- smart pointers were boosted to new dimensions by boost::smart_ptr
without solving the old problems,
http://www.boost.org/libs/smart_ptr/smart_ptr.htm
Best regards,
Roland Pibinger
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Mon Nov 15, 2004 9:36 pm Post subject: Re: smart pointer usage frequency |
|
|
Hi,
christopher diggins wrote:
| Quote: | I am curious about how often C++ programmers today use smart pointers
instead of raw pointers.
|
Since others already responded that smart pointers are used heavily or
even exclusively, I'd like to throw something different to the mix.
I find myself using raw pointers in those cases where the resource
management is already taken care of by *other means*. For example, in
one of my projects I have a set of singleton objects (with classical
instrumentation that forbids free construction and destruction), which
happen to have a common base class with some virtual functions.
In this case, I pass dumb pointers (of the base class type) around.
Please note that this is entirely compatible with the smart-pointer
style: the users do not need to worry about resource management.
Typedefs help to hide the underlying details and differences.
Of course, there is a risk that someone will try to do pointer
arithmetic on such dumb pointers, which would not be possible with
specially prepared pointer type that can only dereference.
But somehow I do not care. ;)
Your comments are welcome.
| Quote: | I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries
|
Simple ignorance combined with too high ego of those who happen to take
decisions and just cannot cross the magic line and admit that listening
to their programmers may help to push the project forward.
| Quote: | Any kind of
relevant anecdotal experiences, or observations would be appreciated.
|
The above was not an anectode. :(
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Alan Griffiths Guest
|
Posted: Mon Nov 15, 2004 9:36 pm Post subject: Re: smart pointer usage frequency |
|
|
"christopher diggins" <cdiggins (AT) videotron (DOT) ca> wrote
| Quote: | I am curious about how often C++ programmers today use smart pointers
instead of raw pointers.
|
It is hard to overstate the value of clear code: a raw pointer gives
no clue as to the intended semantics. A smart pointer type documents
the intent (be it shared ownership, single ownership, forward
iteration, random iteration, locking, etc) and, in addition, can
automate operations such as delete and unlock.
| Quote: | I am also curious as to what kinds of obstacles
still exist with regards to full adoption of smart pointer libraries (i.e.
performance concerns, complexity, laziness, unawareness). Any kind of
relevant anecdotal experiences, or observations would be appreciated.
|
Every C++ project that I've worked on in (at least) the last ten years
has used smart pointers extensively. The only resistance I've seen
was in the early part of that period when they were considered "too
hard to get right". At that time they were all home-grown and written
by the project "guru".
After that, they tended to be a mix of the standard library iterators
and my arglib library (with bespoke additions for such things as
object persistance and locking), more recently boost has replaced
arglib because it is more familiar to the team members.
--
Alan Griffiths
http://www.octopull.demon.co.uk/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Malte Clasen Guest
|
Posted: Mon Nov 15, 2004 9:37 pm Post subject: Re: smart pointer usage frequency |
|
|
Roland Pibinger wrote:
| Quote: | The problem with (some, most) smart pointer approaches is:
- the s.p. is not a pointer (iterator) any more, e.g. different
shared_ptrs cannot point to the same object
- the side effects of a s.p. (e.g. resource handling) often are
unrelated to the pointer (iterator) interface. Different tasks are
lumped together into one class (template). This is a conceptual flaw.
|
I look at it the other way round: When I want to iterate, I use an
iterator. When I want to hold a single object, I use a smart pointer. So
different tasks are perfectly separated whereas raw pointers allow both
and you've got to assure on your own that you do the right thing.
By the way, different (boost::)shared_ptrs can actually point to the
same object, I guess that's why they are called "shared".
Malte
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
christopher diggins Guest
|
Posted: Mon Nov 15, 2004 11:19 pm Post subject: Re: smart pointer usage frequency |
|
|
Hi Roland,
"Roland Pibinger" <rpbg123 (AT) yahoo (DOT) com> wrote
| Quote: | On 14 Nov 2004 05:43:36 -0500, "christopher diggins"
[email]cdiggins (AT) videotron (DOT) ca[/email]> wrote:
Would you mind explaining your position further? For instance why do you
view smart pointers as a passing fad? How are smart pointers an obstacle
to
adoption of a smart pointer?
It is difficult to exactly define 'smart pointer' because any class
that overloads operator* could be seen as smart pointer.
|
Yes, I understand.
| Quote: | In essence, a smart pointer implements a pointer (= iterator)
interface _and_ does something else (has side effects).
The problem with (some, most) smart pointer approaches is:
- the s.p. is not a pointer (iterator) any more, e.g. different
shared_ptrs cannot point to the same object
- the side effects of a s.p. (e.g. resource handling) often are
unrelated to the pointer (iterator) interface. Different tasks are
lumped together into one class (template). This is a conceptual flaw.
|
This an interesting viewpoint which I share to some degree, but you have to
agree that sometimes it is important to merge concerns. The problem as I see
it, is that there should be a representation in code for each concern such
as by multiple bases, multiple fields or policies.
Related to this: I started work on an article for C++ which described two
pointers which do nothing but represent ownership. One pointer represented
ownership (sole or shared, it doesn't matter), and allowed the programmer to
manually delete the memory. The other pointer represented non-ownership and
does not allow deletion of memory. This are more useful than "dumb-pointers"
but are not limited to specific usage cases like smart-pointers, and have no
side effects. Can you see the value in such an approach?
I am also considering the possibility of a policy pointer:
ptr<ownership_policy, deletion_policy>
ownership-policy = no-policy, non-ownership, shared-ownership,
sole-ownership-enforced, sole-ownership-unenforced
deletion-policy = no-policy, manual-deletion, automatic-deletion
Do you think such an approach has potential?
This is a great survey.
| Quote: | Best regards,
Roland Pibinger
|
Thanks for your feedback.
--
Christopher Diggins
http://www.cdiggins.com
http://www.heron-language.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Antoun Kanawati Guest
|
Posted: Mon Nov 15, 2004 11:20 pm Post subject: Re: smart pointer usage frequency |
|
|
Roland Pibinger wrote:
| Quote: | The problem with (some, most) smart pointer approaches is:
- the s.p. is not a pointer (iterator) any more, e.g. different
shared_ptrs cannot point to the same object
- the side effects of a s.p. (e.g. resource handling) often are
unrelated to the pointer (iterator) interface. Different tasks are
lumped together into one class (template). This is a conceptual flaw.
|
The fact is: 'new Blah' gives 'Blah*' and an anonymous memory management
problem. Java and other Garbage Collected languages have solved this
issue by Garbage Collection. In C++, the various forms of
shared-pointers address this issue without the full weight of an
expensive garbage collection solution.
It is very hard to decouple pointers from memory management. So, it
is expected to see that the pointer has acquired extra semantics which
the raw form does not express.
| Quote: | The s.p. waves in the last 15 or so years happend around the following
incidents:
|
It is worth pointing out that fads fade away, and the smart pointer has
not yet. In fact, it is becoming more and more popular. In fact,
garbage collection for C++ has ceased to be 'unmentionnable', and is now
a recurring theme of argumentation.
--
A. Kanawati
[email]NO.antounk.SPAM (AT) comcast (DOT) net[/email]
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|