Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whether to pass shared pointer or raw pointer to a function

Situation

Some third-party API I use works with raw pointers, but I have smart pointers all over my client code.

Problem

When I want to write a wrapper function around the API, I face a problem: Whether to pass shared pointers or their raw pointers underneath as arguments.

Like this:

The smart pointer version


// my wrapper

void MyWrapper(const std::shared_ptr<MyClassA>& pInObj, std::shared_ptr<MyClassB>& out_pOutObj) {

    ThirdParty_DoStuff(pInObj.get(), out_pOutObj.get());
}

// my client code

auto inObj = std::make_shared<MyClassA>();
auto outObj = std::make_shared<MyClassB>();

MyWrapper(inObj, outObj);

The raw pointer version


// wrapper

void MyWrapper(MyClassA* pInObj, MyClassB* out_pOutObj) {

    assert(pInObj && out_pOutObj);
    ThirdParty_DoStuff(pInObj, out_pOutObj);
}

// client code

auto inObj = std::make_shared<MyClassA>();
auto outObj = std::make_shared<MyClassB>();

MyWrapper(inObj.get(), outObj.get());

Question

  • Which is the better approach in terms of performance and memory safety?
  • Will the reference counting work slightly differently between both approaches?

I think that the second version is more reusable if one day the function must work with other kinds of memory management.

like image 538
kakyo Avatar asked Aug 27 '26 05:08

kakyo


1 Answers

I would suggest the following approach to looking at code something like this:

enter image description here

There are some other options like weak_ptr, but for this it is probably not worth looking at.

So for your example, we can see that ThirdParty_DoStuff does not take ownership, so we won't either, so you can choose between a reference and a pointer depending on if the argument is mandatory or not respectively.

EDIT: As of c++17, you have the option of using optional This can also now cover the case where you have an optional output.

like image 154
Fantastic Mr Fox Avatar answered Aug 29 '26 20:08

Fantastic Mr Fox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!