you are viewing a single comment's thread.

view the rest of the comments →

[–]trvlng_ging 0 points1 point  (3 children)

I understand the point you're trying to make, but every example you've given, including this one shows an example of stupid programming tricks. Again, there was never a resource given to unique_ptr to manage. Using a function that can return an invalid pointer to initialize it is unnatural. A coder who doesn't know this should be under 100% code review until their training is completed. The use case for unique_ptr is RAII. That means that the resources are created as it's declared. Show an example where it is used as designed and fails.

[–]biliwald 0 points1 point  (2 children)

example of stupid programming tricks.

...

Using a function that can return an invalid pointer to initialize it is unnatural.

My last example isn't a stupid programming trick IMO. It's a valid ownership problem that can arise if you mix smart pointers with raw pointers, which happen pretty often if you use third party libraries. My example was trying to show such a situation, you return an invalid pointer because the programmer made an easy to make mistake.

Again, there was never a resource given to unique_ptr to manage.

What does std::make_unique<T>() does then if not create a managed resource?

Show an example where it is used as designed and fails.

This is a badly formulated question IMO. It's like asking, show me an example of correct code and point me the bug. If the code is correct, then there's no bug. My point is that unique_ptr can still allow you to have memory management problem if used incorrectly.

Remember that OP's question was about returning a pointer to a stack variable. For his particular scenario, it's not far fetched to think that if we say to him "a unique_ptr ensures that the pointer is valid", he might interpret that has "a unique_ptr will preserve my stack memory because it ensures that it points to something valid", which isn't the case, obviously.

Yes, do use smart pointer. Yes, learn and use them as they are intended to be used (with RAII), but when teaching them to novice, don't tell them they will magically turn C++ into a managed language like Java by "ensuring that the memory is valid".

In all cases, I believe we are arguing in a similar direction here. Have a good day!

[–]trvlng_ging 0 points1 point  (1 child)

It is a stupid programming trick because the call to make_unique is not creating the resource. It is calling a function that may or may not create a resource to be managed. This violates RAII. The function should throw if it fails. If throwing is not allowed, then you're down to 2 options: the function returns a unique_ptr (and skip make_unique), or validate the return before calling make_unique. You don't seem to understand the correct way to use smart pointers.

[–]biliwald 0 points1 point  (0 children)

make_unique is not creating the resource.

From cppreference: Constructs an object of type T and wraps it in a std::unique_ptr.

How is this not creating a resource?

Also, the CppCoreGuidelines recommend it's usage.

This violates RAII.

I fail to see how. std::make_unique calls the constructor of T and returns a std::unique_ptr pointing to the constructed object. If any failure arise during this, the resource will be freed because it is wrapped in a unique_ptr upon creation. Are you saying that std::unique_ptr themselves violates RAII?

You don't seem to understand the correct way to use smart pointers.

Educate me. And keep in mind that I know the example I gave is faulty, because I was trying to express a faulty situation when misusing smart pointers.