C++ Smart Pointers - Do I really need them? When?

แชร์
ฝัง

ความคิดเห็น • 13

  • @roxferesr
    @roxferesr 4 ปีที่แล้ว +2

    Loved this explanation! thanks for the clear examples, they made shared_ptr finally clicked for me. I have one question though, I've seen people passing shared_ptr by reference '&'. When is appropiate to do that, and why would you want to in the first place

  • @mohamedradwan388
    @mohamedradwan388 3 ปีที่แล้ว

    The By Ref example works with my microsoft visual studio compiler. and I am not sure anyway why it should not. why is the struct re-used? the function is invoked with three different parameters, and I suppose each has a different memory location, right?

  • @charlesfleetwood3914
    @charlesfleetwood3914 3 ปีที่แล้ว

    Thank you very useful video.

  • @zemlidrakona2915
    @zemlidrakona2915 4 ปีที่แล้ว +1

    This seems like disaster to me. IMO it's way more confusing and bug prone than doing it the old school way.

    • @filippol1138
      @filippol1138 4 ปีที่แล้ว

      why?

    • @zemlidrakona2915
      @zemlidrakona2915 4 ปีที่แล้ว +2

      @@filippol1138 It just seems like a contrived example. The extra braces are there for no other reason to create the problem in the first place. If you left them out you could use separate variables and construct everything at the top and delete it at the bottom. Even if you want to use std::unique_ptr, you can just call get() on it and pass the normal pointers to the function and it would still release at the bottom. This is a problem I have with "modern" C++. It seems like to goal is often to make simple things more confusing. On top of that since you are using move semantics it will probably create extra machine code to check for the nullptrs in the objects where you moved the pouters out of so it doesn't double de-allocate.

    • @filippol1138
      @filippol1138 4 ปีที่แล้ว +1

      @@zemlidrakona2915 ofc is a contrived example: is just a small example to showcase pointer. In the real world you use them in much larger contexts. The biggest reason to use them is if you are also using exceptions, where tracking all possible paths and ensuring you cleanup everything is very tricky. But even if you do not, you logic will be much simpler: ownership is always clear, you will always know who is supposed to cleanup the pointer; you know there will be *NO* memory leak: as soon as the unique_ptr goes out of scope , it will be freed; you do not have to write deletes everywhere, so you also spare code (and might also spare writing destructors, since you do not have to "delete" when a class owning a pointer is destroyed.

    • @zemlidrakona2915
      @zemlidrakona2915 4 ปีที่แล้ว +1

      @@filippol1138 I know how smart pointers work. I've used them in C++ since the 80s but IMO they should only be used for ownership. You shouldn't need to be passing them around and moving ownership around like this. It seems nonsensical and adds a lot of nullptr checks. Also std::shared_ptr is pretty bad, as it's non-intrusive design requires two pointers where you really only need one.

    • @cppmonk3731
      @cppmonk3731  3 ปีที่แล้ว

      I agree it can be confusing at first, but smart pointers are such a blessing once you start using them. Most companies I've worked with are enforcing use of smart pointers, and it makes sense: I've seen codebases without smart pointers and understanding ownership is such a pain, and extending or using them is so bug prone.
      Another interesting take at smart pointers is how Rust deals with ownership, I recommend having a look there, you may find it more interesting and cleaner than C++.