Skip to content

12.10: Clarification #38

Open
Open
@xjay73

Description

@xjay73

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions