shared_ptr with malloc and free
Asked Answered
B

3

24

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*/

    free(p);
}

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;

tpointer((txn_t*)::malloc(sizeof(txn_t),::free));
Billybillycock answered 4/9, 2012 at 13:36 Comment(3)
Yes, shared_ptr<T> sp((T*)::malloc(...), ::free), depends on how they use the pointer, yes (T* p; fill_p(&p); shared_ptr<T> sp(p, ::free);). I'd suggest using a hand-written scoped_ptr that allows a deleter, though (akin to how std::unique_ptr allows one as a template parameter).Distribute
Sorry but I am not bit clear your example. Do you mean as I allocate memory we will call free. For ExpBillybillycock
The second argument to shared_ptr is the deleter, aka what is called when the pointer should be freed.Distribute
K
35

Can I use shared_ptr with malloc and free.

Yes.

Can anyone point out me sample code base.

You need to provide a custom deleter, so that memory is released using free rather than the default delete. This can be a pointer to the free function itself:

shared_ptr<void> memory(malloc(1024), free);

Remember that malloc and free 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.

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.

I don't quite follow the question. You can use this shared_ptr interchangably with "normal" shared pointers, if that's what you're asking. Type erasure 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 get(); specifically, don't do anything that might free it, since you've irrevocably assigned ownership to the shared pointer.

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.

I guess you mean something like:

double * make_stuff() {
   double * stuff = static_cast<double*>(malloc(whatever));
   put_stuff_in(stuff);
   return stuff;
}

shared_ptr<double> shared_stuff(make_stuff(), free);

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:

void make_stuff(double ** stuff);

double * stuff = 0;
make_stuff(&stuff);
shared_ptr<double> shared_stuff(stuff, free);

How will handle with realloc and calloc

It's fine to initialise the shared pointer with the result of calloc, or anything else that returns memory to be released using free. You can't use realloc, since shared_ptr has taken ownership of the original pointer and won't release it without calling free.

Kanchenjunga answered 4/9, 2012 at 13:55 Comment(2)
I think "double pointer" might mean void allocate(int** p) which would assign *p to new memory. This would be hard to make work with shared pointer I think.Minta
@RasmusStorjohann: That's no harder than with any other way of acquiring the pointer. I've updated the answer, in case that was what was meant.Kanchenjunga
V
6

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*.

Vizard answered 4/9, 2012 at 13:46 Comment(2)
Needs cast, no need for ptr_fun (whatever that is for), and why would .get() be unsafe (OK, unless passed to something that frees it)?Distribute
@Xeo: Thanks for the cast. As I do not know the code of the OP and as he is passing it to C, there is the potential problem, that some code calls free() on the value of .get(), which corrupts the shared_ptr.Vizard
O
1

Can I use shared_ptr with malloc and free. if yes Can anyone point out me sample code base.

Yes. shared_ptr does not allocate memory itself. However, it does delete your object once reference count drops to zero. Since it uses delete by default and you cannot use it with objects allocated by malloc (or any other way), you have to use a custom deleter.

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.

It is not clear what are you asking here. If that function does not expect a shared pointer be passed, then extra care have to be taken. But it depends on what that function actually does.

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.

Yes, you can use any pointer with shared_ptr.

Ouzo answered 4/9, 2012 at 13:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.