Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8482465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:50:14+00:00 2026-06-10T19:50:14+00:00

I have working in large application which contain c and cpp. The all files

  • 0

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));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T19:50:16+00:00Added an answer on June 10, 2026 at 7:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I m working on application in which i have to send all contacts from
I have written a GUI application which after performing some analyses on a large
I have been working on a large java application. It is quite parallel, and
I'm working on a large project (for me) which will have many classes and
I have a large codebase which I am working with which has units like
I have a large-ish project that I'm working on which uses git as the
I have a large simulation written in C++. The main working unit for the
We have a .NET application which our customers consider too large for mass deployment
I'm working on a large Perl application which gets bundled with PAR , along
I have a large application spread across multiple Spring bean definition xml files. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.