473,799 Members | 2,988 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ref to shared_ptr?

are there any pro's and con's in using references to shared_ptr<>'s ?

example:

_______________ _______________ _______________ _____
void obj::foo(shared _ptr<>); vs. void obj::foo(shared _ptr<>&);

and

shared_ptr<>& obj::foo2(); vs. shared_ptr<obj: :foo2();
_______________ _______________ _______________ _____

....?
TIA, ~.rhavin;)
Oct 22 '08 #1
5 7664
On Oct 22, 4:04*pm, ".rhavin grobert" <cl...@yahoo.de wrote:
are there any pro's and con's in using references to shared_ptr<>'s ?

example:

_______________ _______________ _______________ _____
void obj::foo(shared _ptr<>); * vs. *void obj::foo(shared _ptr<>&);
Passing shared_ptr by value causes a call to shared_ptr's copy
constructor just before the function call and a call to the destructor
upon leaving the function. On the other hand, passing by reference
does not involve any constructor/destructor calls (unless it is a
reference to const bound to a temporary object). It depends on an
application whether the difference is noticeable.

Speaking generally, functions that do not acquire or share the
ownership of passed objects should not be taking any smart pointers,
rather they should accept objects by plain reference/pointer.
and

shared_ptr<>& obj::foo2(); * *vs. * *shared_ptr<obj ::foo2();
Performance considerations are the same as for passing shared_ptr to
functions.

Again, one should really think about interfaces and ownership. Is it
expected that the callers of obj::foo2() are going to share the
ownership of the object. If not, it is better to return a plain
pointer/reference.

--
Max
Oct 22 '08 #2
Hi,

..rhavin grobert schrieb:
_______________ _______________ _______________ _____
void obj::foo(shared _ptr<>); vs. void obj::foo(shared _ptr<>&);

and

shared_ptr<>& obj::foo2(); vs. shared_ptr<obj: :foo2();
_______________ _______________ _______________ _____
well, in this case it is up to you to ensure the livetime of the
referenced object.

In general returning a reference to a shared_ptr is a bad advice, since
shared_ptr is explicitly designed to manage object lifetime in
situations like this. And the copy constructor of shared_ptr is quite
cheap, since it is always only a reference to your object.

Remember that when returning a reference to a shared_ptr, the pointer
may no longer point to the same object when you dereference it.
Or, if the shared_ptr instance is ownd by exactly one thread, it will
point to the same object, but the object's lifetime will neccessarily
exceed the lifetime of the shared_ptr in this case. So you could in fact
also return an ordinary pointer (or even better a reference) to your
object. This will more clearly show what is going on.

I think there is only one situation where it is siutable to return a
reference to a shared_ptr: if it is intended that the caller modifies
the shared_ptr (not only the object where it points to).
Marcel
Oct 22 '08 #3
Marcel Müller wrote:
Hi,

..rhavin grobert schrieb:
>______________ _______________ _______________ ______
void obj::foo(shared _ptr<>); vs. void obj::foo(shared _ptr<>&);

and

shared_ptr<> & obj::foo2(); vs. shared_ptr<obj: :foo2();
______________ _______________ _______________ ______

well, in this case it is up to you to ensure the livetime of the
referenced object.

In general returning a reference to a shared_ptr is a bad advice, since
shared_ptr is explicitly designed to manage object lifetime in
situations like this. And the copy constructor of shared_ptr is quite
cheap, since it is always only a reference to your object.
That depends. Incrementing/decrementing reference counts
need to be seen by all processors on multi-processors
architectures, so it requires access to the main memory,
rather than to the cache. Depending on what your program
does, that could be quite expensive.
I do, however, agree that, when you pass a const ref to
a smart pointer, you should probably be passing a const
ref to the object instead. (OTOH, I recently modified a
piece of library cod to use smart pointers instead of
dumb ones and did not evaluate all function signatures
for whether passing an object instead of a pointer would
have been better. Instead I passed smart pointers per
const ref where applicable.)
[...]
Marcel
Schobi
Oct 22 '08 #4
Hendrik Schober <sp******@gmx.d ekirjutas:
Marcel Müller wrote:
>Hi,

..rhavin grobert schrieb:
>>_____________ _______________ _______________ _______
void obj::foo(shared _ptr<>); vs. void obj::foo(shared _ptr<>&);

and

shared_ptr< >& obj::foo2(); vs. shared_ptr<obj: :foo2();
_____________ _______________ _______________ _______

well, in this case it is up to you to ensure the livetime of the
referenced object.

In general returning a reference to a shared_ptr is a bad advice,
since shared_ptr is explicitly designed to manage object lifetime in
situations like this. And the copy constructor of shared_ptr is quite
cheap, since it is always only a reference to your object.

That depends. Incrementing/decrementing reference counts
need to be seen by all processors on multi-processors
architectures, so it requires access to the main memory,
This holds only for smartpointers supporting multithreaded access.
Depending on the application, it might be more useful to consider some
(most?) objects to belong to a certain thread, and to be accessed in
this thread by single-threaded smartpointers. This approach is more
scalable to many cores as no "world-synchronisation " is required for
each refcount update.

Paavo
Oct 22 '08 #5
Hendrik Schober wrote:
Marcel Müller wrote:
>Hi,

..rhavin grobert schrieb:
>>_____________ _______________ _______________ _______
void obj::foo(shared _ptr<>); vs. void obj::foo(shared _ptr<>&);

and

shared_ptr< >& obj::foo2(); vs. shared_ptr<obj: :foo2();
_____________ _______________ _______________ _______

well, in this case it is up to you to ensure the livetime of the
referenced object.

In general returning a reference to a shared_ptr is a bad advice,
since shared_ptr is explicitly designed to manage object lifetime in
situations like this. And the copy constructor of shared_ptr is quite
cheap, since it is always only a reference to your object.

That depends. Incrementing/decrementing reference counts
need to be seen by all processors on multi-processors
architectures, so it requires access to the main memory,
rather than to the cache. Depending on what your program
does, that could be quite expensive.
I do, however, agree that, when you pass a const ref to
a smart pointer, you should probably be passing a const
ref to the object instead.
I guess it depends on what you want to do with the thing you're passing
in. If you're just accessing it, then passing a const ref to the object
makes sense (though arguably no more sense than passing a const ref to
the shared_ptr, if that's what you've got); if you need to store the
object, then passing a const ref to the shared_ptr may be a much better
idea - if you pass a const ref to the object, the only way to store it
is to make a copy, which can be expensive/in some cases (if the copy
constructor is inaccessible) impossible. Copying the shared_ptr, on the
other hand, is generally far less expensive (even if we eschew the
debate about whether or not it is actually 'cheap', for some definition
of cheap).

As a side point, btw, it's worth noting that the const semantics of
passing a const ref to the shared_ptr and a const ref to the object are
different: if you pass a const ref to the shared_ptr, you can change the
object it points to but not the shared_ptr. It's like the distinction
between const X& and X *const. To get the same semantics, you probably want:

const shared_ptr<cons t X>&

It's also worth noting that there's a conversion:

shared_ptr<X-shared_ptr<cons t X>

So you can pass a shared_ptr<Xto a function which expects a
shared_ptr<cons t X>.

Stu
(OTOH, I recently modified a
piece of library cod to use smart pointers instead of
dumb ones and did not evaluate all function signatures
for whether passing an object instead of a pointer would
have been better. Instead I passed smart pointers per
const ref where applicable.)
>[...]
Marcel

Schobi
Nov 1 '08 #6

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

Similar topics

4
1962
by: Ernst Murnleitner | last post by:
Hello, in 2 other threads I had questions partly related to shared_ptr. I changed my normal pointers to a class to shared_ptr. (i.e. boost::shared_ptr). I thought, the use of shared_ptr is more save. But after this my program crashed by several reasons (see below). But maybe there would be an easy solution which I cannot see now.
6
9059
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct type:
14
2045
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let me know how to do it? #include <boost/shared_ptr.hpp> #include <vector> #include <iterator> #include <iostream> class trial {
7
2971
by: myfavdepo | last post by:
Hi all, I have a query regarding the exchanging of a boost::shared_ptr beween different threads. In my program i've two threads both of which having their own internal queues for storing the shared_ptr. one thread is meant to pass the shared_ptr to another after its processing. So when a message arrives i convert it into a class of my own called 'Msg' and i put the 'Msg' object pointer into a shared_ptr and put into the other thread's...
3
2745
by: Tim H | last post by:
I'm newish to C++ but not to C. I'm confused by this code. test1() is fine. test2() fails to compile. /tmp/inherit_ptr.cpp: In function âvoid test2()â: /tmp/inherit_ptr.cpp:52: error: no matching function for call to âboost::shared_ptr<Base>::shared_ptr(test2()::Derived2*)â /usr/include/boost/shared_ptr.hpp:119: note: candidates are: boost::shared_ptr<T>::shared_ptr()
14
6658
by: Tim H | last post by:
I understand the semantics of why this works the way it does. But I wonder if there's a reason for the behaviore at the line marked "QUESTION". I figured if there is an answer, someone here knows it. Specifically, part 1 is obvious to most anyone. Part 2 is isomorphic to part 1, yet behaves differently. If a shared_ptr is const, should it really allow non-const dereferences? Thanks,
9
4732
by: Tim H | last post by:
Why is the following code not valid? I mean, I see the code and it doesn't allow it, but I am curious about the rationale? boost::shared_ptr<intpi = new int; pi = new int; Thanks Tim
4
3964
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
5
3204
by: Fokko Beekhof | last post by:
Hello all, please consider the following code: -------------------------------------------------- #include <tr1/memory> struct BaseA { int x;
0
9686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9540
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10222
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.