我有一个 shared_ptr
指向动态分配的 char
内存缓冲区。让我称其为 shared_ptr
OwnerSharedPointer
。
现在如果我声明:
char * borrowerPtr;
然后这样做:
borrowerPtr = OwnerSharedPointer.get();
并将borrowerPtr
传递给最终使用存储在已分配内存中的数据的某个函数。这有什么问题吗?
最佳答案
Is there anything wrong with this?
可能吧。我可以想到两种情况:
- 函数在指针上调用
delete
。 - 共享指针的引用计数变为 0,并且在使用
borrowerPtr
之前删除指针对象。
关于c++ - 混合 shared_ptr 和普通 ptr,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34875385/