what makes programming languages like go and rust memory-safe and c++ not?

695 views

what makes programming languages like go and rust memory-safe and c++ not?

In: Technology

5 Answers

Anonymous 0 Comments

The “compiler” is a program that converts a language that’s relatively easy for people to read like Go/Rust/C++ into a language that the computer reads, which is called “assembly” or “machine code”. (There can be some other steps in the process but we’ll stick with the simplest answer.)

The C++ compiler doesn’t enforce a lot of rules. You can write code that is very obviously flawed and it won’t complain. This is a blessing and a curse. It means very clever C++ developers can do ridiculous things and yield performance benefits for it. It also means developers who aren’t as clever as they think can create serious problems.

Rust and Go have more “discipline”. They have rules about how you are supposed to use memory. If your code breaks those rules, the compilers refuse to convert your code to machine language. Sometimes the bad stuff happens in C++ because the code makes the status of some memory ambiguous. A stricter compiler’s response to ambiguous memory use is to treat it like an error until you remove the ambiguity and prove you’re following the rules.

Imagine you’re playing the Shell Game with someone. This is the game where a ball or some small object is placed below one cup, and several identical cups are placed next to it. Then, the person running the game slides the cups around to try to disguise where the ball is. If you pick the cup with the ball, you win.

C++ is like a version of that game where anything goes. The person might spin the table around and, while you’re not looking, take the ball away so there’s no way to win.

Rust and Go are like a version where the person running the game can’t leave, can only make 5 swaps, and you also win if he’s sneaky and takes the ball away. Under these rules, you’re much more likely to win because the game’s operator can’t do cheaty things.

So why isn’t C++ more like Rust and Go? Well, history.

First, if people are very careful they can write code that’s just as “safe” in C++. It just takes an awful lot of care.

Second, when C++ was being developed, we didn’t know as much about what isn’t “safe”. Computer Science is a pretty young field, roughly 60-80 years old by certain definitions. We had to make a lot of mistakes to learn what mistakes look like.

Third, compilers are very complex and can take up a lot of computing power. The computers we have today are hundreds of thousands of times more powerful than they were when C++ compilers were developed. It may not have been possible to implement the features Rust and Go enforce *and* maintain feasible compilation times.

We could update C++ compilers today, but it’s usually hard and dangerous to dramatically change how such an old compiler works. There is a LOT of C++ code written that might stop working if the compiler suddenly required everything to be safe. That could be really bad, since that code probably does a lot of important things and the cost of reworking it to be “safe” could be immense. There are tools and compilers that do perform more rigorous checks, and I’m sure people use them. But if a C++ compiler suddenly *required* it, that would be a very unpopular change and people would refuse to use that compiler.

You are viewing 1 out of 5 answers, click here to view all answers.