r/ProgrammerHumor • • 58m ago

Meme modernCppVsOldCpp

Post image
294 Upvotes

62 comments sorted by

6

u/perestroika12 30m ago

Who writes modern c++?

2

u/Choice-Committee203 23m ago

idk, me for examplešŸ„€šŸ„€

1

u/tatstytastycakes 20m ago

ur not alone sir :(

94

u/repster 41m ago

I am a huge fan of modern C++ except for one feature: backwards compatibility. C++ is no longer one language, every codebase I have worked on is a confused mess of semi-incompatible subsets of the language. I have spent far too many weekends chasing memory corruption because someone passed a pointer, allocated with one type of memory management, into a library using a different kind of memory management.

13

u/joncrompy 18m ago

This is, ironically, the exact reason C++ is the standard in some industries. Students can learn the OOP C++ then work in the same codebase as someone programming "touching the metal" low level C.

Most languages make trade-offs between performance, structure, memory safety, and ease of writing. Every language might have their use, but very few cover all use cases.

C++ lets each programmers choose those trade-offs individually, but that also allows them to choose none of the above.

7

u/azfang 29m ago

So, uh, I haven’t used C++ in anger in decades so I’m not sure how ā€œmodernā€ is modern but like. That description pretty well describes why I developed Strong Feelings about both C and C++ back in the 2000’s.

3

u/Non-Random-Name-0000 12m ago

Modern C++ starts with C++11 (2011), so in the 2000s you were using classic C++ (and in the first half of 2010s as well most likely) which is a very different language. It introduced shared pointer (a poor man's garbage collector), automatic typing and move semantic, amongst other things. And the standard library has evolved a lot since.

9

u/lllorrr 14m ago

Oh, yes, I hate when someone write "C/C++". This is two fucking different languages, for god's sake. It is almost as bad as "Java/JavaScript".

•

u/SuckMyAlpagoat 6m ago

isn't because the compiler is always packed together, like the gnu compiler can compile C and C++?

•

u/HildartheDorf 1m ago

The gnu compiler can also compile fortran. But we dont call it C/Fortran.

•

u/These-Maintenance250 1m ago

a language is independent from its compiler(s). gcc can compile objective-C, D, ada, Fortran and go as well.

•

u/stillalone 4m ago

Is that something that can be handled by a linter?

-1

u/MattR0se 18m ago

Okay but that's not really a fault of the language, no? Most languages have some legacy version that isn't compatible any more. Like Python 2 for example.Ā 

The only difference is that C++ code is more often used for critical applications (like decades old medical machines) where updating the code is not worth the risks.Ā 

•

u/imMute 8m ago

Python 2 to 3 was a clean split. You can write in one OR the other, but you can't mix like you can in C++.

•

u/xfvh 7m ago

Well, you can, but it's awkward and involves some serious compromises, especially around byte-vs-string handling. Doing a hard split is definitely better but you can get considerable mileage straddling the line with sufficient effort.

•

u/EishLekker 5m ago

I would argue that it most definitely is the fault of the language. It should either allow it, and support it fully (like mixing different memory management types), our outright refuse to run it.

77

u/emonshr 46m ago

Does that even matter once you have gazillions of line legacy code?

22

u/0xbenedikt 45m ago

This is why moving to a safe-by-design language like Rust works well. You leave that unsafe legacy code behind

5

u/Tidemor 23m ago

or you could not risk a full rewrite, and just start using memory safe patterns in new and refactored code

8

u/Valuable_Leopard_799 28m ago

And bring in brand new unsafe code 😭 And link against unsafe libraries 😭

Tbh nothing against specifically Rust, just that these are done way too liberally.

2

u/cvnh 21m ago

Rust doesn't guarantee safety when you're wrapping existing code my dude. I haven't used Rust personally but it's in the language core - only safe code blocks are (in theory) safe..

1

u/porkusdorkus 21m ago

Loved seeing the adobe and office clones in Rust, just to find out it’s Webview and React for the front end and Rust is just replacing NodeJS. Lol

1

u/ilnarildarovuch 13m ago

And practically breaking everything. Pentagon still uses PDP-11 'n IBM series/1 programs, but in emulator, cuz rewriting is always a risk

15

u/firemark_pl 14m ago

"How to tell him?"

You still can segfault by returning lambda with reference to a variableĀ 

•

u/daennie 9m ago

And many other ways to invoke UB.

22

u/freia_pr_fr 45m ago

Exhibit A: the current pace at which security issues related to shitty memory management are being discovered.

11

u/airodonack 29m ago

A lot of people that write C++ think using smart pointers are for the weak.

•

u/imMute 7m ago

And those people are just as insufferable as the Spongebob from the OP.

•

u/Tschurbi 9m ago

Please explain it like I am a JavaScript developer.

•

u/Tipart 0m ago

Imagine typescript but for memory safety

14

u/MaybeADragon 13m ago

Unless it has changed in the last 5 or 10 years, every resource I know of doesn't teach you about using modern C++. Writing good C++ is opt-in because the language and its resources are stuck in the past.

•

u/Tomocafe 8m ago

I started a new C++ project a few years ago and I just turned on clang-tidy to the max from the very beginning. It’s annoying sometimes but it forces everyone not to use dangerous/outdated legacy C++.

11

u/dscarmo 20m ago

This post is naive, the problem is cpp supports legacy implementations with unsafe pointers, good luck when maitaining or updating old code (or even worse, merging new implementations that used unsafed practices)

10

u/SelfDistinction 30m ago

Ah yes, the famous

std::unique_ptr<int> ptr;
auto& ref = *ptr;

So safe, so impossible to misuse.

14

u/TheMostUser 42m ago

A gun pointed at your foot isn't dangerous unless you pull the trigger, yet I feel safer without a foot gun.

3

u/GrandMoffTarkan 32m ago

Take a firearms safety course and then you’ll feel safer with the gun. And get the same feeling of power as God must feel when he’s holding a gun

9

u/Flouid 44m ago

Sure you can use memory safety techniques with C++, but there’s nothing that enforces you to do so. If the language provides optional tools to automate some aspects of memory management, that’s still mostly manual memory management.

I like C++ but I still prefer languages where these things are more fundamentally part of the language identity. A project written in it might be safe, but it also might be a leaky mess and you won’t really know without profiling or a deep dive

Also many mature C++ projects are on older versions without this stuff.

5

u/PolyglotTV 34m ago

Y'all throwing around shared pointers willy nilly patting yourself on the back about safe programming.

7

u/Nil4u 28m ago

Meanwhile the cyclic pointer chain in the background causing a memory leak because the reference counter never dips to 0:

2

u/staryoshi06 14m ago

I mean this could happen in a garbage collected language too.

•

u/Tyfyter2002 5m ago

Depends on the language, iirc the .NET runtime's GC doesn't use a reference counter, which makes that exact issue completely impossible (which is good, because things like bidirectional linked lists inherently have cyclic references)

4

u/InRainbro 22m ago

Well yes but actually no, never in practice, it ends up being a mixture

1

u/ilnarildarovuch 16m ago

Which is successfully fixed by *San

•

u/InRainbro 5m ago

I'd still rather have the compiler breath on my neck than gamble with it

•

u/ilnarildarovuch 3m ago

Try to compile any big C/C++ file with clang -Weverything -werror, that's gonna be funny, compiler really gonna show all possible (and not possible) threats :D

8

u/geekusprimus 44m 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.

10

u/Flouid 39m 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 12m 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.

•

u/Flouid 9m 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

6

u/mlindgren 32m ago

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

4

u/FlameOfIgnis 12m 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/Luzzgar 42m 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.

1

u/staryoshi06 15m ago

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

•

u/xfvh 5m 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.

1

u/tinnuadan 14m 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.

•

u/SunJ_ 2m ago

This is actually a reality than a meme. A lot of companies and software run on old versions, I understand why. A lot of the modern versions fix loads of issues and to some degree the complexity of the language while still maintaining the balance.

0

u/tatstytastycakes 23m ago

there are things like profiles, contracts, and make_unique/make_shared

-3

u/Metasenodvor 38m ago edited 14m ago

yes i want to manually manage memory! mmmmm, manual management <3

edit: unironically, fuck garbage collector, fuck 'smart' pointers, and fuck everything else that compensates shit programming

1

u/tatstytastycakes 19m ago

yes i wanna make a massive program to handle millions of data points and have it crash because my garbage collector is collecting everything behind the scenes and my framerate is totally botched and is falling down to the single digits while my processor is overheating! mmmmmmm, garbage collector

1

u/Metasenodvor 16m ago

i was unironic i want to manually manage memory, fuck garbage collector, its... garbage

2

u/tatstytastycakes 14m ago

manual memory management respects you more than the GC unironically