Smart pointer confusion (std::unique_ptr in function argument)

Pages: 12
Thanks for the explanations and example guys.

But I am still seeing a lot of code (including mine until now) not using smart pointer, just raw pointer with new operator and delete operator usually in the destructor, there a reason for that?

PS: the question above goes beyond the scope of the OP so it is ok if you don't answer.
shared_ptr, unique_ptr and make_shared were introduced in C+11. make_unique came in C++14.

Any code written before then used new/delete (or auto_ptr which has now gone). Also, much of C++ teaching involves new/delete rather than smart pointers. Indeed some C++ teaching seems to be only c changed to be 'C++ like'.

Why did your code until recently use new/delete instead of smart pointers - because you didn't know about them possibly?

What're taught initially tends to be what you stick with until you learn otherwise...
A lot people are unwilling to change to newer, better ways to do things. The old ways work, though at a potential cost of continuing maintenance, and learning to use newer methods is not worth the effort apparently.

Or they simply don't know, nor care, what's available.

Using the C libraries for generating random numbers when C++ has a vastly larger suite of tools available for example.

https://en.cppreference.com/w/cpp/numeric/random

https://www.learncpp.com/cpp-tutorial/introduction-to-random-number-generation/

https://web.archive.org/web/20180123103235/http://cpp.indi.frih.net/blog/2014/12/the-bell-has-tolled-for-rand/

https://open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3551.pdf

I followed the suggestions in that working group paper and created a simple header-only namespaced custom library I can include whenever I need trivial-use random numbers.

I recently expended a bit of brain juice to convert the .hpp header to a .cppm module interface file for my C++20 apps, a quickie 15 minute rewrite.

Legacy code will always be with us, just accept that. Adapting and updating has real world costs.

I am not a professional paid-to-program worker toiling on years old code bases. I am a self-taught hobbyist. Learning the new stuff C++ offers, and from time to time seeing how professional programmers do things fills me with awe and inspiration, is what keeps me plugging away.

I can afford to improve my knowledge and how-to-use-C++ since all the coding I do is for learning and personal enjoyment. None of it is "gonna make the company go bankrupt if the app isn't ready or is buggy" mission critical.
I am still seeing a lot of code (including mine until now) not using smart pointer,

it also depends on where you look.

C++ was made to solve practical engineering problems and code that actually dealt with those problems, industrial code, had smart pointers going all the way to early 1990s. Can't say I did a large survey but every company I worked for in early 2000s had them in code bases (before switching over to boost), both shared_ptr and unique_ptr were first submitted for standardization back in 1994 (then called "counted" and "scoped"), Marshall Cline's C++ FAQ in 1991-1996 discussed smart pointers, and Alexandrescu's book from 2000 had five or six kinds of smart pointers discussed as accepted practice.

On the other hand, college education has no need for robust code or engineering practices, they focus on computer science elements instead (DS/algo). So the code that comes out of educators, their books, and their students, lacks smart pointers or any other industry-compatible concepts.
Last edited on
And what is taught at the uni/college level gets filtered down to the public schools by students who become instructors and/or curriculum administrators as "The One True Way" how C++ must be taught and used.

Cubbi wrote:
C++ was made to solve practical engineering problems

"Why I Created C++"
https://bigthink.com/videos/why-i-created-c/

It was developed for more than solving engineering problems.
> C++ was made to solve practical engineering problems

I think 'engineering' here refers to 'software engineering'.
In my case the first experience the first book I read didn't teach smart pointer, only in second book I learned about them.
But in my short run with c++ I never really had to use memory allocations in my programs. I started using Qt and there I am using new operator a lot but I use the parent mechanism there that takes care of deletion.

So just few days ago I started working on migrating code from a very old version of Wt(https://github.com/emweb/wt) to newest version and found that the api has changed a lot and now they use smart pointer every where.

I kinda understand the effort and reluctance to change, even if its to better stuff. If you have something that works and you understand how it works it is hard for some people to do it.

In languages like Java we have the garbage collection which is said to slow down the language a lot, how the smart pointers differ from garbage collection? Doesn't slow down c++ as well?
hbcpp wrote:
how the smart pointers differ from garbage collection? Doesn't slow down c++ as well?
Bjarne likes to say C++ doesn't produce garbage, so there is nothing to collect..
Or, more technically, C++ has destructors, which is where resources (including memory) are released instead of leaving it up for the background GC/finalizer threads.
Smart pointers are a bit of a red herring here; unique_ptr<T> is no different from std::vector<T> or std::unordered_map<K,V>; it allocates memory as needed, and releases it all in one go (and recursively tells the elements to release their memory).

If it's an object with large heap footprint, like vector<vector<vector<T>>>, that's a lot of deallocations to run in one destructor, upon ending a function call or otherwise leaving the scope where that vector lives. Java people may say their approach is better because it's spreading that same cost over a bunch of background threads so the function that created the massive data structure can return immediately, without slowing down the thread it is running on.. but in practice it is never that idyllic and even newest GCs can stop the world. Not to mention loss of determinism and extra painful non-memory resource management.
Last edited on
Thanks for the explanation, I get it now.
"I saw the light, I saw the light
No more darkness, no more night
Now I'm so happy, no sorrow in sight"

:) :)
Topic archived. No new replies allowed.
Pages: 12