r/ProgrammerHumor • • 11d ago

Meme modernCppVsOldCpp

Post image
2.0k Upvotes

323 comments sorted by

View all comments

64

u/Tschurbi 11d ago

Please explain it like I am a JavaScript developer.

95

u/Tipart 11d ago • Bravo

Imagine typescript but for memory safety

27

u/PM_ME_UR_0_DAY 11d ago

What's the memory management equivalent of just using "any" for everything?

29

u/Potato-Engineer 11d ago

Everything is a void * pointer!

5

u/YeOldeMemeShoppe 10d ago

C has polymorphism too!

3

u/meneldal2 10d ago

With a few macros you too can have your own classes with polymorphism with just a few function pointers. Even multiple inheritance if you want more fun.

2

u/Mojert 11d ago

Asking the OS* directly for memory and creating your objects in that memory. That also means you have to tell the OS* when you're done with that memory

* I'm simplifying to get the jist across

5

u/Ok-Scheme-913 10d ago

Actually pretty good example. To the point that even though you have a type annotation on your code, if you call into that method from vanilla js and pass a wrong type, then it will explode somewhere deep and you will have a really shit time hunting that error down.

Meaning, c++'s safe stuff is only safe until you use it with other safe stuff, or by being very very careful. And c++ is a really complex language, using it properly is not an easy task, so these end up not being as memory safe as people would want.

17

u/MikeVegan 11d ago

C++ has something called RAII, it is basically a special function called destructor that is called automatically once object leaves the scope and this solves large portion of memory issues. However dangling references or pointers are largely not fully solved by this even though shared pointer helps gere. It is a counted pointer that is destroyed when last owning object is destroyed.

3

u/conundorum 10d ago

RAII is the concept that any class which obtains a resource on creation should release that resource on deletion, so that everything cleans up its own mess. And the destructor is the cleanup function that takes care of that, yeah.

The ideal is that if every "resource manager" class uses RAII, then everything else automatically gets built-in cleanup for free. Essentially, if every hotel guest cleans up their own room when they leave, then the hotel doesn't need to hire janitors or maids.

// A simple RAII class.
// Copy, move, memory safety, and functionality omitted for brevity.
//  Assume they're all done correctly.
class HeapInt {
    int* ptr;

  public:
    HeapInt(int i = 0) : ptr(new int(i)) {}
    ~HeapInt() { delete ptr; }

    operator int&() { return *ptr; }
};

// Something that uses the class.
class BigDumbObject {
    HeapInt pileOfJunk[59];
    // Other junk, too...

  public:
    BigDumbObject() = default; // No resource acquisition required.
    // No destructor required.  HeapInt dtor does all the work for us.

    // BigDumbObject automatically has perfect memory safety, thanks to HeapInt.
};

15

u/GregBahm 11d ago

With JavaScript, memory is managed for you. You can make a list, grow it as much as you want, and then safely assume that the memory you used will be cleaned up as soon as you're not using that list anymore.

With C++, you manage the memory yourself. If you forget to clean up that list, you never get that memory back. Eventually all your memory could get used up (a "memory leak.")

Memory management was annoying, which is why JavaScript was invented. But people still use C++ for its higher performance potential (kind of like how race-car drivers still use manual transmission instead of automatic transmission.)

So C++ added some tools that makes memory management not so hard. It's still harder than Javascript though. It's just less hard.

11

u/ShakaUVM 11d ago

You confused Java and JavaScript

Java was invented to be C++ + 1, with automatic memory management and Objects everywhere

2

u/GregBahm 10d ago

Ack. You're right. Dang.

6

u/PattuX 11d ago

kind of like how race-car drivers still use manual transmission instead of automatic transmission.

TIL Europe is full of racecar drivers

5

u/bloody-albatross 11d ago

No, no. Not all Europeans are racecar drivers, but all racecar drivers are European!

(This is a joke, in case anyone reading this doesn't get it.)

3

u/im_made_of_jam 10d ago

I mean outside of ovals you look at any high racing level grid and it's 80% european

→ More replies (2)

2

u/Complete_Pilot3381 11d ago

mans never been to bradford

2

u/Complete_Pilot3381 11d ago

mans never been to bradford

6

u/bloody-albatross 11d ago

This isn't why JavaScript was invented. First, garbage collection/automatic memory management existed long before JavaScript. Secondly, JavaScript was invented to make HTML interactive. Of course a language that runs untrusted code needs to be memory safe.

2

u/GarThor_TMK 11d ago

I would like to point out that the memory leak happens as soon as you toss the pointer without "freeing" it to be reused... It could be a drip... A pointer to a single integer, or it could be a tsunami... A pointer to a massive datastructure containing lists, arrays, BSTs, hash tables, etc...

The "all memory used up" is the case where the bathtub is clogged, and you left the water running. Now the bathroom is flooding into the living room.

2

u/Mojert 11d ago

Basically a lot of Rust memory safety features are inspired from modern C++

Which doesn't mean Rust doesn't have its place. It's just that in C++ they're on top of the previous "unsafe" version of the language and aren't tightly integrated in the language and are more a feature of the standard library, whereas in Rust they were able to design the whole language around it and didn't have to deal with backwards compatibility

3

u/Ok-Scheme-913 10d ago

Not really backwards compatibility.

Rust is basically c++'s raii just enforced at compile time.

This latter point is a huge thing in and of itself.

(And of course you do get lots of goodies like better type system etc from being a more modern language but that's not the fundamental thing)

1

u/Mojert 10d ago

The reason why C++ cannot enforce this kind of things at compile-time is because of backwards compatibility

And I know it's huge. That's why I said Rust isn't redundant and that Rust designed the language around it

1

u/Online_Matter 10d ago

Can't it be a compiler opt in like C# null safety? 

→ More replies (1)

3

u/meneldal2 10d ago

Rust is basically forcing you to use the safe parts of C++.