You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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 justp
as the argument, becausep
is already ashared_ptr
, so passing justp
creates a copy and increments the counter just fine. The reason this is important is that ifp
had been defined as a built-in pointer instead, thenprocess(shared_ptr<int>(p))
would be a problem, because the call would create ashared_ptr
pointing to the same memory that the built-in pointerp
does and set its count at 1 (not 2), so the memory pointed to by the smart pointer would be deleted whenprocess
is finished, resulting in the built-in pointerp
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.
The text was updated successfully, but these errors were encountered: