shared_ptr vs pass object by reference
If I can pass the object by reference to a function, then is there a
need to use shared pointers? I demonstrated 3 ways of passing by
reference below, but my main concern is just passing a shared_ptr by
value VS passing an object reference.
#include<iostream>
#include
If I can pass the object by reference to a function, then is there a need to use shared pointers?
Not if you know the lifetime of your object and it won't be deleted before the function has finished. You may find scoped pointers useful though: http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/scoped_ptr.htm
On 7/28/10 9:33 AM, in article 9FA45EF1AB90804BB8FF5A4A8AD0B97305A895A801@IE2RD2XVS031.red002.local, "Patrick Loney" wrote:
If I can pass the object by reference to a function, then is there a need to use shared pointers?
Not if you know the lifetime of your object and it won't be deleted before the function has finished. You may find scoped pointers useful though: http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/scoped_ptr.htm
I use shared_ptr like a "poor-man's" garbage collection. Instead of having to remember to "delete" or "free" my pointers I can wrap them in a shared_ptr and know that when the last reference to the pointer goes out of scope the pointer will be cleaned up. In fact it is better if you pass a shared pointer by value otherwise the reference count will NOT be correct which may or may not have bad side effects. Maybe I am using shared_ptr wrong but at least for my project it seems to work well. *I did come from a Java Background before C++ so this type of programming model is natural to me for better or worse. Mike Jackson
If I can pass the object by reference to a function, then is there a need to use shared pointers?
Not if you know the lifetime of your object and it won't be deleted before the function has finished. You may find scoped pointers useful though: http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/scoped_ptr.htm
I use shared_ptr like a "poor-man's" garbage collection. Instead of having
Ahh, being a c++ developer I see garbage collection as a poor man's raii (you can't even control when something is destructed for gods' sake ;))
Maybe I am using shared_ptr wrong but at least for my project it seems to work well.
Scoped_ptr offers the automatic memory management you discuss without the additional functionality of shared_ptr (i.e. shared_ptr is a scoped_ptr with added reference counting functionality)
On 7/28/10 11:50 AM, in article 9FA45EF1AB90804BB8FF5A4A8AD0B97305A895A964@IE2RD2XVS031.red002.local, "Patrick Loney" wrote:
If I can pass the object by reference to a function, then is there a need to use shared pointers?
Not if you know the lifetime of your object and it won't be deleted before the function has finished. You may find scoped pointers useful though: http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/scoped_ptr.htm
I use shared_ptr like a "poor-man's" garbage collection. Instead of having
Ahh, being a c++ developer I see garbage collection as a poor man's raii (you can't even control when something is destructed for gods' sake ;))
Not sure if "Garbage Collection" was the best use here. A better term in my mind should have been "Memory Management". Thanks for the sanity check on RAII. I'll review my architecture again to make sure we are using the best for our project.
Maybe I am using shared_ptr wrong but at least for my project it seems to work well.
Scoped_ptr offers the automatic memory management you discuss without the additional functionality of shared_ptr (i.e. shared_ptr is a scoped_ptr with added reference counting functionality) Thanks. I'll take a look.
Mike Jackson
I'm a little bit confused with scoped_ptr. Can I still pass that
pointer by value into a function like I did with shared_ptr in the
above example? I just tried it and it doesn't compile. From what I
understand scoped_ptr is only used inside a function and shared_ptr is
for passing objects into functions without the developers who work on
the code worrying about reference counts, no? Please correct me if I'm
wrong.
Max
On Wed, Jul 28, 2010 at 11:50 AM, Patrick Loney
If I can pass the object by reference to a function, then is there a need to use shared pointers?
Not if you know the lifetime of your object and it won't be deleted before the function has finished. You may find scoped pointers useful though: http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/scoped_ptr.htm
I use shared_ptr like a "poor-man's" garbage collection. Instead of having
Ahh, being a c++ developer I see garbage collection as a poor man's raii (you can't even control when something is destructed for gods' sake ;))
Maybe I am using shared_ptr wrong but at least for my project it seems to work well.
Scoped_ptr offers the automatic memory management you discuss without the additional functionality of shared_ptr (i.e. shared_ptr is a scoped_ptr with added reference counting functionality) _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Can I still pass that pointer by value into a function like I did with shared_ptr in the above example?
No, you can't take a copy of a scoped ptr, when one goes out of scope it automatically causes the pointed to object to destruct, if there are two scoped ptrs pointing to the same object what would happen when the second is then used to access the already destructed object? You can pass scoped ptrs by reference, use shared pointers when you want multiple objects to have ownership of the pointed to object (i.e. no object can destroy the contents of the shared pointer while another object still needs it).
Cool, thanks! I thought I was missing something. I think I have to use
shared_ptr then, because my code is shared among various parallel
OpenMP workers... I'll post here again if my parallelization doesn't
work out. The arrays are sliced, but I have other data structures
which keep track of these slices, and those are better off with
shared_ptr IMO.
Max
On Thu, Jul 29, 2010 at 5:10 AM, Patrick Loney
Can I still pass that pointer by value into a function like I did with shared_ptr in the above example?
No, you can't take a copy of a scoped ptr, when one goes out of scope it automatically causes the pointed to object to destruct, if there are two scoped ptrs pointing to the same object what would happen when the second is then used to access the already destructed object? You can pass scoped ptrs by reference, use shared pointers when you want multiple objects to have ownership of the pointed to object (i.e. no object can destroy the contents of the shared pointer while another object still needs it). _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I use shared_ptr like a "poor-man's" garbage collection. Instead of having
Ahh, being a c++ developer I see garbage collection as a poor man's raii (you can't even control when something is destructed for gods' sake ;))
Perhaps, but shared_ptr's may fail here either. Atm shared_ptr to object being constructed (thru enable_shared_from_this) cant be dealt out in constructors which make it impossible to build parent child relation ships at constructor time if the child needs a weak_ptr to parent.
participants (4)
-
gast128
-
Max S. Kaznady
-
Michael Jackson
-
Patrick Loney