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 4125710
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:54:19+00:00 2026-05-20T23:54:19+00:00

I am going to use heavily some C-API in my C++ application, I have

  • 0

I am going to use heavily some C-API in my C++ application, I have a lot of functions available that I need to call in couple. For example createFoo(void*) at the beginning and freeFoo(void*) when the job is done.

So, I thought to use the RAII idiom, so I created the first wrapper with copy constructor and assignment operator private and not implemented. It works fine, however I would like a richer copy semantic. I would like to use a reference counting copy semantic. I have considered to write my own version but I don’t want reinvent the wheel. Also the boost::shared_ptr is already implementing pretty much the behave that I want to achieve.

The only difference is the pointer is not create with new and it is not release with delete. I would like to customize the source and the sink functions.

I have the feeling that is a good way to face the problem, however I cannot come up with a class that implement my idea.

Here are the questions, do you think it is a good way to solve my problem? Is there any open source code that implement something similar? Do you have any hints?

Thanks

  • 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-05-20T23:54:20+00:00Added an answer on May 20, 2026 at 11:54 pm

    It’s really easy. As an example, suppose you have the C fopen interface:

    FILE* fopen(blah);
    int fclose(FILE*);
    

    Then you can wrap FILE resource with shared_ptr like this:

    shared_ptr<FILE> ptr(fopen("file.txt", "rt"), fclose);
    // use the file pointed by ptr
    fwrite(..., ptr.get());
    
    // FILE is automatically closed by a call to fclose when 
    // reference count drops to zero...
    

    Edit: Let me explain a bit.shared_ptr stores a ‘deleter’ along with the reference counters for ownership and weak ownership. A deleter is any object that can be called with the pointer to the resource to be freed as an argument. The default deleter simply calls delete on its argument, however you can pass your own deleter.

    When I apply the technique above, I usually wrap the creation into a separate function. This simplifies the code and allows me to check for error-codes returned from C-style APIs and convert them to exception:

    shared_ptr<FILE> MakeFile(const char* name, const char* mod)
    {
        if(FILE *ptr = fopen(name, mod))
            return shared_ptr<FILE>(ptr, fclose);
    
        throw SomeExceptionType("fopen", errno, ...);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to JSON, and I need to parse some that an API
I'm going to use some content from an AppleScript dictionary server-side. I need to
I am going to use the DAAB to prevent a lot of manual labor
We have been going back and forth a lot around our office lately about
I'm going to use SQLite in order to save a lot of data in
we are going to use mongo db for an alert monitoring application. We thought
Note: I’m going to use a specific object as an example here, but please
I'm going to use a time interval to call function autoSave(). Inside the function
A little background I'm working on an .net application that's uses plugins heavily, the
I am going to use Vaadin UI framework to develop web application where indexing

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.