Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

12.10: Clarification #38

Open
xjay73 opened this issue Jul 14, 2021 · 0 comments
Open

12.10: Clarification #38

xjay73 opened this issue Jul 14, 2021 · 0 comments

Comments

@xjay73
Copy link

xjay73 commented Jul 14, 2021

It's worth noting that while the call process(shared_ptr<int>(p)) works fine in this example, it's not necessary to use anything more than just p as the argument, because p is already a shared_ptr, so passing just p creates a copy and increments the counter just fine. The reason this is important is that if p had been defined as a built-in pointer instead, then process(shared_ptr<int>(p)) would be a problem, because the call would create a shared_ptr pointing to the same memory that the built-in pointer p does and set its count at 1 (not 2), so the memory pointed to by the smart pointer would be deleted when process is finished, resulting in the built-in pointer p becoming a dangling pointer.

Therefore, it's probably better to avoid phrasing the call in this way altogether so that we don't learn something that would be a bad habit to get into.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant