unique_ptr handling for double pointer
<p>I'm using a C function that has a signature like:</p>

<pre><code>/**
 * Read values from source file and store in a newly 
 * allocated array
 */
size_t load_array(FILE * source, double ** pdest) {
  size_t values_read;
  double * dest;
  // ...
  // Keep reading in values from source, incrementing values_read
  // resizing dest when needed.
  // ...
  *pdest = dest;
  return values_read;
}
</code></pre>

<p>which currently gets called in my C++ code like this:</p>

<pre><code>double * my_array;
size_t array_length = load_array(source, &amp;my_array);
// ... do stuff with my_array ...
free(my_array);
</code></pre>

<p>Can I wrap up <code>my_array</code> with <code>std::unique_ptr</code> so that <code>free</code> gets automatically called? I cannot change the C function (which is part of an external library) so I cannot change the data structure used therein.</p>

<p>There is a <a href="https://stackoverflow.com/questions/27440953/stdunique-ptr-for-c-functions-that-need-free">similar question on SO</a>, but in that question the C function returned a pointer, and a <code>unique_ptr</code> was created around this returned value. Some of the suggested answers would work if a dumb pointer was created first and then wrapped later, e.g.:</p>

<pre><code>double * my_array;
size_t array_length = load_array(source, &amp;my_array);
auto my_array_wrapper = std::unique_ptr&lt;double, decltype(free)*&gt;{ my_array, free };
// ... do stuff with my_array_wrapper ...

// free() called on wrapped pointer when my_array_wrapper goes out of scope
</code></pre>

<p>This doesn't seem very clean to me, since I would still have the original pointer lying around in my code. What I would like to do to completely contain the pointer, with something like:</p>

<pre><code>clever_ptr my_clever_array;
size_t array_length = load_array(source, my_clever_array.get_ptr_address());
// ... do stuff with my_clever_array ...

// free() called on wrapped pointer when wrapper goes out of scope
</code></pre>

<p>Obviously I could write a small class for this, but I'm interested in whether there are existing utilities that already provide for this.</p>