用malloc和free shared_ptr的 [英] shared_ptr with malloc and free

查看:505
本文介绍了用malloc和free shared_ptr的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包含C和CPP大型应用程序有工作。所有文件保存为CPP扩展,但code是写在C-风格。我的意思是定义结构,而不是通过类的malloc和realloc和calloc.In最近他们已经安装Boost库分配内存,所以我打算使用到我现有的code基础,所以我有一些以下问题。


  1. 我可以使用std :: shared_ptr的使用malloc和free。

  2. 如果是的,任何人都可以指出我的样品code基?

  3. 如果将我在应用程序中创建的std :: shared_ptr的影响它的任何功能,这指针传递给另一个函数,它使用的malloc或释放calloc?

或者换句话说:

我如何实现与的std :: shared_ptr的类似功能,对下列code:

 无效allocateBlocks(INT ** PTR,为int * CNT)
{
    * PTR =(INT *)malloc的(的sizeof(int)的* 10);
    * CNT = 10;
    /*做一点事*/
}诠释的main()
{
    为int * p = NULL;
    诠释计数= 0;
    allocateBlocks(安培; P,&放大器;计数);
    /*做一点事*/    自由(对);
}

我们调用一些功能,其中接受双指针,并填写自己的应用程序内的结构和使用malloc。我们可以将这些指针到std :: shared_ptr的?例如:

  typedef结构txn_s
{
    int类型;
    INT D组;
    INT * E;
} txn_t;TYPEDEF提高:: shared_ptr的< txn_t> tpointer;tpointer((txn_t *)malloc的::(的sizeof(txn_t)::免费));


解决方案

  

我可以使用的shared_ptr用malloc和free。



  

任何人都可以指出我的样品code基地。


您需要提供一个定制删除,从而使内存被释放使用免费而不是默认的删除。这可能是一个指向免费函数本身:

 的shared_ptr<无效>内存(的malloc(1024),免费);

记住的malloc 免费只能处理原始内存,与您​​正确地创建和销毁要负责任何不平凡的对象,你可能想要保留在内存。


  

如果我在应用程序中创建的shared_ptr,如果他们使用malloc或释放calloc这个指针传递给另一个函数。它会影响任何功能。


我不太明白的问题。您可以使用此的shared_ptr 互换与正常的共享指针,如果这就是你在问什么。 <青霉>类型擦除的确保指针用户不会受到不同类型的删除的。正如任何共享指针,你有,如果你用提取获取原始指针()来小心一点;特别是,不要做任何可能免费吧,因为你已经无可挽回地分配所有权的共享指针。


  

我们必须调用一些功能,接受双指针,并填写自己的应用程序内的结构和使用malloc,我们可以将这些指针shared_ptr的。


我猜你的意思是这样的:

 双* make_stuff(){
   双*的东西=的static_cast&LT;双* GT;(的malloc(其他));
   put_stuff_in(东西);
   返回的东西;
}shared_ptr的&LT;双&GT; shared_stuff(make_stuff(),免费);

更新:我没有当场把双指针中,我假设你指的是C风格的使用指针来模拟一个参考来模拟一个返回值;你可以做到这一点:

 无效make_stuff(双**的东西);双*的东西= 0;
make_stuff(安培;东西);
shared_ptr的&LT;双&GT; shared_stuff(东东,免费);


  

将如何与realloc的和释放calloc处理


这是罚款释放calloc 的结果来初始化共享指针,或其他任何返回内存使用被释放免费。不能使用的realloc ,因为的shared_ptr 采取了原始指针的所有权,而不调用不会释放它免费

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));

解决方案

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.

这篇关于用malloc和free shared_ptr的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆