News for hackers. Stuff that matters.
Re:Smart pointers and move semantics in modern C++ by Iv on Monday September 26, 2016 @04:11AM (#12580101)
I use both C++ and C# a lot these days, and like both. I am more comfortable with C++ so memory management is usually not a problem, interestingly I find that it is in C# that it is sometimes annoying to wonder if the objects you manipulate are references or values, but you end up getting the gist of it after a bit of practice.

The prevailing rule in C++ is to try to avoid using pointers when you can, you should rather use references. It is surprising the amount of cases when this is enough.

A lot of people use smart pointers in C++, which implement reference counting transparently for a little overhead. shared_ptr was made official in C++11. It allows to make pointers on an object and delete it when all pointers were destroyed.

I personally prefer to do without smart pointers and make objects ownership explicit: If I need to have long-lived objects I will put them in a structure/array/container and comment about it being the original owners. I would then do allocation/deallocation manually, which actually makes me more comfortable, as I prefer it to be explicit.

But that is purely a matter of taste.

Re:Smart pointers and move semantics in modern C++ by int_19h on Monday September 26, 2016 @04:19AM (#12580135)
Not all smart pointers are reference counting - unique_ptr is not, and it has zero overhead compared to a raw pointer. Sometimes you just need to heap allocate, or, say, shove a bunch of objects of different derived types in a same container.
Re:Smart pointers and move semantics in modern C++ by Iv on Monday September 26, 2016 @12:36PM (#12583292)
Yes sorry, I conflated smart pointers and shared_ptr. I rarely use the other kinds of smart pointers.
Re:Smart pointers and move semantics in modern C++ by hellofunk on Monday September 26, 2016 @04:50AM (#12580285)
In fact, in my tests, unique_ptr with make_unique was faster than a raw pointer with new.