简体   繁体   English

为什么将原始指针转换为unique_ptr不会删除原始指针?

[英]Why does casting a raw pointer to a unique_ptr not delete the original?

I am trying to understand unique_ptr, but they have me a bit stumped. 我正在尝试了解unique_ptr,但是它们让我有些困惑。 Consider the following code: 考虑以下代码:

PartClass * part= new PartClass;

//The OwnerClass is a composite that excpects a unique_ptr
OwnerClass * owner = new OwnerClass( unique_ptr<PartClass>( part );

After the second line of code I would expect the original pointer - part - to have been deleted. 在第二行代码之后,我希望原始指针(部分)已被删除。 After all, as soon as you have a unique_ptr it creates a lot of fuss, needing to be moved, etc. So why does the compiler allow a raw pointer to still access the part-object? 毕竟,一旦有了unique_ptr,它就会引起很多麻烦,需要移动等等。那么,为什么编译器允许原始指针仍然访问零件对象? Doesn't it violate the whole concept of unique_ptr? 它是否违反了unique_ptr的整个概念?

It works for the same reason that you can explicitly give out the pointer. 它的工作原理与您可以明确给出指针的原因相同。 For example: 例如:

std::unique_ptr<int> value = std::make_unique<int>(1);
int* pValue = value.get();

While pValue can access the memory it does not own it and should not delete the memory. 尽管pValue可以访问该内存,但它并不拥有该内存, 因此不应删除该内存。 In your example the ownership has been transferred while there happens to be a raw pointer that still points the the resource, which is fine. 在您的示例中,所有权已经转移,而恰好有一个原始指针仍指向资源,这很好。

A unique_ptr is just another class from the compiler standpoint. 从编译器的角度来看, unique_ptr只是另一个类。 As it stands, one of its ctors accepts a raw pointer. 就其现状而言,其ctor之一接受原始指针。 It's dtor will release the memory via passed in raw pointer (typically free ). 它的dtor将通过传入的原始指针(通常为free )释放内存。

The fact your variable part points to the same memory location does not change any of the unique_ptr behaviour. 您的可变部分指向同一存储位置的事实不会更改任何unique_ptr行为。 In this context that is just another local object. 在这种情况下,这只是另一个本地对象。

The fact you do hold a raw pointer to the same memory managed via unique_ptr (order does not matter) may potentially lead to a double free, and undefined behaviour (hopefully a crash). 实际上,您确实拥有指向通过unique_ptr管理的同一内存的原始指针(顺序无关紧要),这有可能导致双重释放和未定义的行为(希望是崩溃)。 I'd recommend passing new PartClass directly to unique_ptr initialization. 我建议直接将new PartClass传递给unique_ptr初始化。

By passing part to the unique_ptr you passed across ownership of the pointer to unique_ptr . 通过将part传递给unique_ptr您跨过了指向unique_ptr的指针的所有权。

Yes, you've still got a raw pointer, but as you've given ownership to the smart pointer you should consider it off limits. 是的,您仍然有一个原始指针,但是当您赋予智能指针所有权时,应该考虑使其超出限制。 You certainly don't want to be deleting it. 您当然不想删除它。

The PartClass object still exists, and that's what was intended, so it absolutely should not be deleted. PartClass对象仍然存在,这正是预期的目的,因此绝对不应删除它。 The question is really why isn't part set to a null pointer, and that's because it's up to you to manage such things. 问题的实质是为什么不将part设置为空指针,这是因为管理此类事情由您决定。 The pointer is valid, and you can use it if you want to. 该指针是有效的,并且您可以根据需要使用它。 If you don't want to use it, don't keep it around. 如果您不想使用它,请不要随身携带。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM