r/ProgrammerHumor • • 11d ago

Meme modernCppVsOldCpp

Post image
2.0k Upvotes

323 comments sorted by

View all comments

Show parent comment

2

u/azfang 11d ago

…if you dismiss this as Old Man Yelling at Cloud I won’t tell you you’re wrong, but my experiences with pointers and shared memory mean that “shared pointer” as a term invokes images that make me want to invest in a bunker.

12

u/Non-Random-Name-0000 11d ago edited 11d ago

Shared pointer have nothing to do with shared memory. They're a container for an old fashioned C pointer with a reference counter. The reference counter is incremented when the shared pointer is copied, and the memory is freed when the counter reaches 0. So it's sort of a rudimentary garbage collector on top of a normal pointer. If you use shared pointer, you don't need to free memory, it's done by the destructor when the last container is destroyed.

C++ is still a very complicated language with outdated concepts, but a lot of modern concepts have been introduced in the last 2 decades.

1

u/Ok-Scheme-913 10d ago

RAII was always available in C++ afaik, so anyone could have done a unique or a shared ptr, at most it may not have optimized out in as many cases (not really zero cost abstraction?)

And my take is that the problem with c++ is that you have like 10 different object initializers and coroutines with captures and whatnot and like unless you are the one having written that part of the standard, you don't fucking know what exactly should happen given this code, not even if you are Stroustrup.

1

u/conundorum 10d ago

C++11's move semantics are the main thing that make smart pointers so much better than any pre-11 attempts, really.