简体   繁体   English

将解引用的智能指针的地址传递给需要原始指针的函数

[英]Passing the address of dereferenced smart pointers to functions that expect raw pointers

(assuming that I am working with a library or framework that expects usage of raw pointers,) (假设我正在使用一个需要使用原始指针的库或框架,)

Is it valid practice to use a smart pointer which owns some data, and then pass the address of the derefenced smart pointer to a function that expects a raw pointer?使用拥有一些数据的智能指针,然后将解除保护的智能指针的地址传递给需要原始指针的函数是否有效?

Yes, that is valid practice.是的,这是有效的做法。 The std smart pointers have a get() member function exactly for that purpose. std智能指针具有完全用于此目的的get()成员函数。

In general, when you manage an object through smart pointers, you should only pass the whole smart-pointer-object as is to other functions when these functions imply ownership semantics: if a function will copy a std::shared_ptr , it should accept it by value.一般来说,当你通过智能指针管理一个对象时,你应该只将整个智能指针对象按原样传递给其他函数,当这些函数暗示所有权语义时:如果一个函数将复制一个std::shared_ptr ,它应该接受它按价值。 Similar for std::unique_ptr .类似于std::unique_ptr More often than that, a function doesn't have anything to do with ownership, it just wants to act on data and/or behavior passed into it.更常见的是,一个函数与所有权没有任何关系,它只是想对传递给它的数据和/或行为采取行动。 Then, your first choice should be to take a ( const -qualified) reference, because it doesn't have the additional nullptr -state of pointers.然后,您的第一选择应该是采用( const限定的)引用,因为它没有额外的nullptr指针状态。 Otherwise, a pointer is just fine.否则,一个指针就好了。

Long story short: if you deal with an API that accepts raw pointers and doesn't do any ownership-related actions on it (delete it, copy the pointee), then it's fine to pass .get() to it.长话短说:如果您处理的 API 接受原始指针并且不对其执行任何与所有权相关的操作(删除它、复制指针),那么将.get()传递给它就可以了。

As long as the function doesn't expect to take ownership of the data, definitely.只要函数不希望获得数据的所有权,当然。

In fact, that is also how you should design your own functions: use a smart pointer in an interface if, and only if, it should participate in the ownership of the pointee.事实上,这也是你应该如何设计你自己的函数:当且仅当它应该参与指针对象的所有权时,在接口中使用智能指针。

Is it valid practice to use a smart pointer which owns some data, and then pass the address of the derefenced smart pointer to a function that expects a raw pointer?使用拥有一些数据的智能指针,然后将解除保护的智能指针的地址传递给需要原始指针的函数是否有效?

Yes, that is potentially a valid practice... as long as that function doesn't take ownership of that raw pointer.是的,这可能是一种有效的做法……只要该函数不拥有该原始指针的所有权。 However, it is important to take note how long the passed pointer will be used.但是,重要的是要注意传递的指针将使用多长时间。 The lifetime of the smart pointer must match or exceed the use of that pointer.智能指针的生命周期必须匹配或超过该指针的使用时间。

In case the function does take ownership, then it may instead be a valid practice to pass an address release d from the smart pointer, but only if the deleter matches what the framework would do with the pointer.如果函数确实获得了所有权,那么从智能指针传递地址release d 可能是一种有效的做法,但前提是删除器与框架对指针的处理相匹配。

The above answers are correct, I 'd only want to add a minor catch: When it's about threaded execution.上面的答案是正确的,我只想添加一个小问题:当它是关于线程执行时。 Then you should be extra-careful about functions that take raw pointers because these may become invalid while executing and the other stack has released them.那么你应该格外小心那些使用原始指针的函数,因为这些函数在执行时可能会变得无效并且其他堆栈已经释放了它们。

When designing your own functions that will go threaded, it might be better to use std::shared_ptr even if the function could use a raw pointer.在设计自己的线程函数时,即使该函数可以使用原始指针,也最好使用std::shared_ptr

struct foo = {...};
void testfoo(foo* msg)
{
}

if (1)    
{
shared<foo> f = std::make_shared<foo>(...);
std::thread t(testfoo,f.get());
t.detach();
}  // whops. f destructed perhaps before testfoo could manipulate it

Better:更好的:

void testfoo(std::shared_ptr<foo> msg)
{
}

if (1)    
{
shared<foo> f = std::make_shared<foo>(...);
std::thread t(testfoo,f);
t.detach();
} // f has been copied, so no pointer release

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

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