shared_ptr with malloc and free

后端 未结 3 392
清歌不尽
清歌不尽 2021-01-31 04:35

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

3条回答
  •  遥遥无期
    2021-01-31 05:10

    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.

提交回复
热议问题