Get pointer pointer for smart pointer

I have a smart pointer to an object that I need to pass to a method that accepts only a pointer to a pointer.

Here is an example where the original smart pointer loses ownership.

int main(int argc, char* argv[])
{
    std::unique_pointer<pcap_if_t> object;

    auto object_pointer = object.get();
    pcap_findalldevs(&object_pointer, ...); // The unique_ptr loses ownership after being passed to the function
}

How to do this if the original smart pointer has not lost the right to the pointer?

EDIT:
The function I'm calling is pcap_findalldevs in libpcap. I am afraid that this feature may cause loss of property.
I updated my sample code to reflect what I'm actually doing.

+4
source share
3 answers

, , , , pcap_findalldevs . pcap_freealldevs.

, - unique_ptr :

struct pcap_deleter
{
    void operator()(pcap_if_t* ptr) 
    {
        pcap_freealldevs(ptr);
    }
};

//...
using pcap_ptr = std::unique_ptr<pcap_if_t, pcap_deleter> 
pcap_ptr get_devs() {
    pcap_if_t* object_pointer;
    pcap_findalldevs(&object_pointer, ...); 
    return pcap_ptr(object_pointer);
}

//...

auto object = get_devs();
+6

, .

. _ptr get()

unique_ptr release()

: http://en.cppreference.com/w/cpp/memory/unique_ptr

+3

. :

 pcap_if_t ip = nullptr;
 res = pcap_findalldevs(&ip, errbuf);
 // now ip (hopefully) points to something

- . ip - . , .

, ip , , . . , ++ .

+1

Source: https://habr.com/ru/post/1627342/


All Articles