Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Can I use shared_ptr with malloc and free.</p> </blockquote> <p>Yes.</p> <blockquote> <p>Can anyone point out me sample code base.</p> </blockquote> <p>You need to provide a custom deleter, so that memory is released using <code>free</code> rather than the default <code>delete</code>. This can be a pointer to the <code>free</code> function itself:</p> <pre><code>shared_ptr&lt;void&gt; memory(malloc(1024), free); </code></pre> <p>Remember that <code>malloc</code> and <code>free</code> only deal with raw memory, and you're responsible for correctly creating and destroying any non-trivial objects you might want to keep in that memory.</p> <blockquote> <p>if I create shared_ptr in my application and pass this pointer to another function if they are using malloc or calloc. will it impact any functionality.</p> </blockquote> <p>I don't quite follow the question. You can use this <code>shared_ptr</code> interchangably with "normal" shared pointers, if that's what you're asking. <em>Type erasure</em> ensures that users of the pointers aren't affected by different types of deleter. As with any shared pointer, you have to be a bit careful if you extract the raw pointer with <code>get()</code>; specifically, don't do anything that might <code>free</code> it, since you've irrevocably assigned ownership to the shared pointer.</p> <blockquote> <p>We have call some function which accept double pointer and fill the structure inside their application and use malloc Can we assign those pointer to shared_ptr.</p> </blockquote> <p>I guess you mean something like:</p> <pre><code>double * make_stuff() { double * stuff = static_cast&lt;double*&gt;(malloc(whatever)); put_stuff_in(stuff); return stuff; } shared_ptr&lt;double&gt; shared_stuff(make_stuff(), free); </code></pre> <p>UPDATE: I didn't spot the phrase "double pointer", by which I assume you mean the C-style use of a pointer to emulate a reference to emulate a return value; you can do that too:</p> <pre><code>void make_stuff(double ** stuff); double * stuff = 0; make_stuff(&amp;stuff); shared_ptr&lt;double&gt; shared_stuff(stuff, free); </code></pre> <blockquote> <p>How will handle with realloc and calloc</p> </blockquote> <p>It's fine to initialise the shared pointer with the result of <code>calloc</code>, or anything else that returns memory to be released using <code>free</code>. You can't use <code>realloc</code>, since <code>shared_ptr</code> has taken ownership of the original pointer and won't release it without calling <code>free</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload