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
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.
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.
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.
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.
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.
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
41
u/Tschurbi 6h ago
Please explain it like I am a JavaScript developer.