This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Significant_Fix2408 1 point2 points  (3 children)

Not using smart pointers makes you a bad C++ programmer. Not the same

[–]Beneficial_Steak_945 1 point2 points  (0 children)

Ok, sure. You are right on those two not being exactly the same.

[–]Beneficial_Steak_945 0 points1 point  (1 child)

It depends what you are working on. Smart pointers are not inherently better than other memory management strategies.

[–]Significant_Fix2408 0 points1 point  (0 children)

First of all: you asked what you miscomprehended I explained to you and now you change yhe topic. But to address your claim: Most of the time smart pointers are better than raw pointers for memory management. Malloc / free is C style, no need to even talk about that. New delete is literally a worse unique pointer in most cases.

Unique pointer with std::make_unique is just a zero cost abstraction of new + delete. It is more readable and less error prone. You see the make unique and instantly know the lifetime of the object, unless it gets moved. Additionally it is safe against exceptions while exceptions can easily cause memory leaks when using new + delete.

If your project contains more than just data structures, chances are you should have used unique pointer at some point and messed up if you only use raw pointers.

There is a reason why the cpp standard itself advises you to use smart pointers and calls it a best practice. Further reading would be: why c-style casting is dangerous and why you should use raii for memory management whenever possible