Smart pointers proper usage in a simple program

Hi,

The following is a theoretical project whereby I struggle to get through the challenge of ambiguity of using smart pointers correctly!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

std::shared_ptr<int> foo() {
	std::shared_ptr<int> pn(new int{ 4 });
	return pn;
}

int main() {

	std::shared_ptr<int> pn(foo());
    std::cout << *pn << '\n';

	system("pause");
	return 0;
} 


Whether I use a unique_ptr or shared_ptr, the output is alike without any errors. Which one should I use for this project and why, please?

Last edited on
You use unique_ptr when ownership is held by a single entity that has complete control of the lifetime of the object. You use shared_ptr when ownership is shared by multiple entities.

In the above example, either one will work, therefore you should prefer unique_ptr, since it has a simpler implementation.
You use unique_ptr when ownership is held by a single entity that has complete control of the lifetime of the object.
Is that single entity the object pn on line 4? Because it's the name of the pointer.

You use shared_ptr when ownership is shared by multiple entities.
On line 10, that object (the pointer) is assigned to another pointer, is it not sharing the ownership?

Last edited on
Is that single entity the object pn on line 4? Because it's the name of the pointer.
You can think of it that way. I think it's more useful to think that the entities owning the pointer at different points in time are main() and foo().

On line 10, that object (the pointer) is assigned to another pointer, is it not sharing the ownership?
It's not being assigned, it's being moved. One function is transferring ownership to another function. Here, ownership is also not shared:
1
2
auto p = std::make_shared<int>(42);
std::shared_ptr<int> p2(std::move(p));
1) The owner of a smart pointer is the block/function in which that pointer is created, right?

2) Yes, it's moving, I just didn't notice it well. Is moving that pointer to another in a different function (main()) not sharing the ownership? Because pn on line 10 and 11 owns the content and prints it.

3) Or how to know where a smart pointer is shared, for instance, to keep matters simple, in the project above?
> Which one should I use for this project
for that particular example, dynamic allocation is not needed
perhaps you should provide a proper example, one that does exemplify your issue.

> The owner of a smart pointer is the block/function in which that pointer is created, right?
¿what do you understand by «owner»?

> Is moving that pointer to another in a different function not sharing the ownership?
when you share, both parties may be able to use the resource
if you are in main(), then foo() already died, so it can't own anything (also, I find weird to say that a function owns something)

> how to know where a smart pointer is shared
¿look at the ref_count?
a shared pointer may be shared
a unique pointer is unique and may not be shared


¿what are you trying to model?
Last edited on
> Which one should I use for this project and why

You should not be using a smart pointer at all here. std::vector<> is a clearly better choice; with move semantics, these can be efficiently returned from a function.

Registered users can post here. Sign in or register to post.