News for hackers. Stuff that matters.
Re:Mistakes to avoid with C++ 11 smart pointers by userbinator on Sunday May 15, 2016 @01:42AM (#11699618)
As someone who uses more C than C++, I have often wondered about the value of "smart" pointers and other pseudo-automatic memory management abstractions, since in any case one has to understand the traditional manual memory management to use them correctly, and if they do, they then also have to understand the additional semantics of the class implementing it too --- or bugs like the ones shown in this article will result.
Re:Mistakes to avoid with C++ 11 smart pointers by hellofunk on Sunday May 15, 2016 @03:50AM (#11699954)
Unique_ptr offers many advantages:

1) Exception safety: what if you throw between your new and delete? Unique_ptr will clean up for you anyway and leave you in a stable state.

2) What if you forget to delete or your delete code never runs? Any of the many problems associated with explicitly writing delete can be non-issues with unique_ptr.

3) Clarity. When you see regular pointers in C or C++, it is never immediately clear who owns them. With unique_ptr, you don't have this problem because it doesn't look like a pointer. If you therefore see pointers, you know immediately they are non-owning.

4) Convenience functions like reset() on unique_ptr handle the release of the old and allocation of the new simultaneously. In general, the less code you have to write is often the better path to safety and maintainability.

5) Unique_ptr handle resources other than memory. It can be used to handle file streams, DB connections, etc and automatically close those also, if you provide a custom deleter function (which can just be a lambda, for elegance).

6) Unique_ptr is not copyable. This is important: if you own resources, it force you to consider this when you copy objects, and the compiler will force you to write your own copy constructor. Without this, it is easy to get into trouble if you forget these things with raw pointers.

7) Many syntactic improvements: i.e. unique_ptr will automatically call delete or delete[] based on what needs to happen, you can't accidentally get it wrong.

This is just a start list. The value of unique_ptr is considerable.

Re:Mistakes to avoid with C++ 11 smart pointers by zvrba on Sunday May 15, 2016 @02:35AM (#11699782)
Most important advantage: RAII. You cannot forget to free the pointee even in the presence of exceptions. The "bugs" described in the article are just a case of not having read the manual. (Like, who in their right mind would assume that wrapping something in a shared_ptr would make it thread-safe?!)
Re:Mistakes to avoid with C++ 11 smart pointers by random_upvoter on Sunday May 15, 2016 @07:06AM (#11700435)
I totally agree. In most software projects, memory management isn't a particularly difficult aspect of the problem. With todays software tools, if your code contains memory leaks or pointer bugs you're just being an amateur. And in those cases where memory management requires a bit more thought and design it's rather unlikely that some standard smart pointer protocol is going to be a glove-fit solution. So I never saw the point of smart pointers unless you want to reinvent Python or something.
Re:Mistakes to avoid with C++ 11 smart pointers by pjmlp on Sunday May 15, 2016 @10:05AM (#11700936)
I develop software since the mid-80s.

Never been part of a C or C++ project at the enterprise level, where any single developer could answer any question about memory management tracking.

Tracking down crashes due to memory mismanagement was always a several days task.