Smart pointer and pass by reference | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Smart pointer and pass by reference

What is best between Shared_ptr by reference Or Unique_ptr Is it any case when unique_ptr does not work and only shared_ptr by reference is the only choice ?

2nd Jun 2022, 4:14 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
7 Answers
+ 2
Imagine that you need to share a resource to 3 different classes. If you make a pointer to the resource and share the reference you will not sure when you can delete it because you dont know if a class is using that reference yet. If you make a unique_ptr you can only pass by reference or move it to each class. That class cant take that reference as an own member due to unique_ptr non-copy policy. The best solution for that is share the reference as a shared_ptr. You can set the reference as a member in each class and the reference will be automatically deleted when the last class finishes using it. That is the purpose of shared_ptr: share the same resource without make physical copies of them taking care on its deletion when it is no longer used.
9th Jun 2022, 8:12 PM
Victor Cortes
Victor Cortes - avatar
+ 1
Depends on what you really need. Shared_ptr costs more than a simple unique_ptr due to the reference count on each copy. The copy is disabled in unique_ptr, so of you want to pass a unique_ptr as a parameter you need to do so passing as a reference (unique_ptr<T>&), sharing the unique_ptr::get() (the raw pointer) or moving it (the best choice, using the move semanthics std::move) I don't like to use shared_ptr as an alternative to unique_ptr because they have different purposes: unique_ptr forces the pointer to have only one owner. It is light (sizeof unique_ptr<T> = sizeof T*) and fast. Shared_ptr in other hand can be copyed to make the same reference between one or more owners, costing more memory and more processing to control the pointer state.
6th Jun 2022, 12:03 PM
Victor Cortes
Victor Cortes - avatar
+ 1
It's reverse way Victor Cortes If we can observe in code a shared_ptr which is not assigned and just passed as reference to different function calls , is it safe to have unique pointer and move the same while calling function and catch the updated pointer as function return ?
8th Jun 2022, 3:46 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
But shared pointer is costly compared to unique pointer.... If both my implementation has no advantage over other, is it safe to go ahead with unique pointer and then move it and get it again from function return ? Is there any harm doing so ? Now if this is safest , then why share pointer reference is there ? What is use case when share pointer reference is better than what i am thinking about unique pointer ?
9th Jun 2022, 6:02 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I am afraid I didn't understand your question. Can you show some example of what you are talking about?
8th Jun 2022, 4:51 PM
Victor Cortes
Victor Cortes - avatar
0
I just created a basic sample code as below : https://code.sololearn.com/cDaxZatOL4jS/?ref=app Now question is about test1 and test2 Is it good to choose amongst either of these two ? Or there is advantage of one over other ?
8th Jun 2022, 5:58 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
There is no advantage between shared and unic when you pass by reference because you are not copying anything. If you try to pass by value you will get an error with unique but it will work fine with shared.
9th Jun 2022, 5:22 AM
Victor Cortes
Victor Cortes - avatar