The code is error. The memory pointed by smart pointer sp
is freed by delete p;
, and sp
becomes a dangling pointer. It will cause some problems. We cannot dereference any one of the shared pointers which point to the same memory that sp
pointed, and when all these shared pointers are destoryed, the memory will be freed again.