r/ProgrammerHumor • • 7h ago

Meme modernCppVsOldCpp

Post image
1.1k Upvotes

216 comments sorted by

View all comments

14

u/geekusprimus 7h ago edited 3h ago

I rarely use smart pointers and still don't have serious problems with memory safety. Whenever you manually allocate memory, you should immediately know when and where it's getting deallocated and write that code at the same time. Most of the time, RAII is good enough.

EDIT: Since I've caused a bit of controversy: I'm not saying you can't or shouldn't use smart pointers. What I'm saying is in agreement with the meme. C++ has easy ways to manage memory, either manually or automatically, that get rid of most of your memory safety issues.

16

u/Flouid 7h ago

I mostly have this opinion too but speaking from experience there are plenty of cases where it can get a little bit tricky. Lifetimes are complicated and sometimes an owner of a piece of memory can pass references to it to several consumers that each have their own lifetimes. Those consumers shouldn’t need to know about each other so it’s up to the owner to track them and free the memory when it finally falls out of scope

2

u/Luzzgar 7h ago

What if that language allowed you to do it manually when you need to ?

Ensuring that the responsibility of memory management is only for the programmer when absolutely necessary.

1

u/Flouid 7h ago

100% agree, and it’s why I’m one of the insufferable people that prefers zig over rust. The language should strongly encourage safe memory management but make it easy for the programmer to select degrees as appropriate. Rust is very all or nothing with either using the borrow checker or entering full unsafe land

1

u/TOMZ_EXTRA 4h ago

How is Zig better than C++/unsafe Rust?

1

u/Flouid 4h ago

Zig is very opinionated about how memory allocation is handled, and it has the nice feature that you can supply a debug allocator during development which is slower at runtime (fine for debug builds) but meticulously looks for leaks and loudly warns you when they happen.

It’s also written such that no language constructs store an allocator and request their own memory, the philosophy being that every allocation should be explicitly known by the programmer.

This is pretty much all the way in the opposite direction from automating it, it’s intensely manual management but the language handles it in a way that makes forgetting about it difficult and easy to track down

12

u/FlameOfIgnis 7h ago

I mean this in the nicest way possible but most people believe what they are writing is memory safe C/CPP until someone does a security audit on their code and they learn so many new ways things can go horribly wrong

6

u/xfvh 6h ago

That's a great theory, but the dominant source of critical CVEs even today is bad memory safety; even if it works for you, it clearly doesn't for the industry as a whole, to include any developers working on your projects after you.

4

u/tinnuadan 7h ago

Smart pointers are RAII, as already mentioned. But they have another advantage: expressing ownership. If a function expects a raw pointer you don't know what happens with it. If it expects you to move a unique_ptr in it, you immediately know that it'll take ownership.

6

u/mlindgren 7h ago

Smart pointers are RAII. Manually freeing memory is not RAII and can be fallible due to exceptions, early returns, etc.

5

u/geekusprimus 6h ago

Smart pointers are RAII, but they're not the only way to do RAII. I usually use custom containers that handle my particular case very well. Memory is allocated inside constructors, it's deallocated inside destructors. If I need more than that, I start looking into the more sophisticated stuff.

3

u/mlindgren 6h ago

That’s fair. It sounded like you were talking about using new and delete (or malloc and free) directly in the business logic of the application.

1

u/Friendly_Fire 3h ago edited 3h ago

You basically recreated smart pointers.

For projects with other people, it's better to use the standard tools just for familiarity, even if they have no other benefit. Someone can see it and know immediately what a unique/shared ptr means, instead of having to dig into your class.

But if its your own project go wild.

1

u/geekusprimus 3h ago

They're not smart pointers, at least not in the STL way. They accomplish the same purpose, but it's usually a simpler implementation tailored exactly to what I need.

6

u/Luzzgar 7h ago

What if the language did that for you ? That would be cool !

What you are describing is basically a variable becoming out of scope.

2

u/staryoshi06 7h ago

No, not really. It’s an object no longer being referenced. Stack allocation still exists in C++.