shared_ptr with malloc and free


To use a std::shared_pointer with malloc() and free() you should specify a custom deleter. This should do it

std::shared_ptr<T> ptr(static_cast<T*>(malloc(sizeof(T))), free);

As long as you do not pass around the result of std::shared_ptr<T>::get(), your pointer is safe.

Edit: Added casting the result of malloc() to T*.


I have working in large application which contain c and cpp. The all files saved as cpp extension but the code is written in c- style. I mean it is define structure rather than class allocate memory through malloc and realloc and calloc.In recent They have installed boost library So I am planning to use into my existing code base So I have some following question.

  1. Can I use std::shared_ptr with malloc and free.
  2. If yes, can anyone point out me sample code base?
  3. Will it impact any functionality if I create std::shared_ptr in my application and pass this pointer to another function, which uses malloc or calloc?

Or in other words:

How do I achieve the similar functionality with std::shared_ptr, for the following code:

void allocateBlocks(int **ptr, int *cnt)
    *ptr = (int*)malloc(sizeof(int) * 10);
    *cnt = 10;
    /*do something*/ 

int main()
    int *p = NULL;
    int count = 0;
    allocateBlocks(&p, &count);
    /*do something*/


We call some functions, which accept double pointer and fill the structure inside their application and use malloc. Can we assign those pointer to std::shared_ptr? For example:

typedef struct txn_s
    int s;
    int d;
    int *e;
} txn_t;

typedef boost::shared_ptr<txn_t> tpointer;


make shared_ptr not use delete

Or how about using the stl to provide the wrapper functor - Doug T. description but without the custom caller.

boost::shared_ptr<T> ptr( new T, std::mem_fun_ref(&T::deleteMe) );
boost::shared_ptr<S> ptr( new S, std::ptr_fun(lib_freeXYZ) );

Doug T. answered your question nicely. I'll tell you about intrusive_ptr. Maybe you can use it in your project too.

If you have some C library that has already reference counting, but you have to manually call those functions, you can use boost::intrusive_ptr too, and provide proper definitions for its add_ref and release functions. intrusive_ptr will find and call them. They are responsible to increment the reference count and decrement it, freeing the resource when necassary:

void intrusive_ptr_add_ref(foo *f) {

void intrusive_ptr_release(foo *f) {
    if(lib_dec_ref(f) == 0) 

Then you can just create objects from raw pointers of type foo*. intrusive_ptr will call your functions when its copied/destructed:

intrusive_ptr<foo> f(lib_alloc());

// can wrap raw pointers too, which already may be referenced somewhere else
foo *p = get_foo_from_somewhere();