DEV Community

Discussion on: Modern C++ Isn't Scary

Collapse
 
aminmansuri profile image
hidden_dude • Edited

What's scary about C++ is not the syntax. Yeah it looks a bit weird these days and all but that's not the problem. (I programmed in C++ for 10 years)

The problem is best illustrated with books like "Effective C++" and "More Effective C++" aka. "The 100 things you have to bear in mind to avoid inadvertently creating a memory leak".

With the addition of more features in the language I'm pretty sure they've created another 100 things that you need to bear in mind to avoid implicit memory leaks. So the surface area of expertise needed to do this increases.

At some point it just becomes easier to select a cleaner language.

Collapse
 
serbuvlad profile image
Șerbu Vlad Gabriel

This is exactly it!! There seem to be 2 ways to do memory: the first is to let the developer handle memory like in C. The second is to let the language handle memory, like in GC languages or in Rust.

If you let the developer handle memory that's all fine. Using malloc() and free() is actually easy once you get used to it. You have to figure out when objects get created and when they get destroyed, and plan things out accordingly, but it generally works out. Which is why memory leaks in C programs are rare and, for good code, easy to fix.

Letting the language handle the memory is also fine. You get a performance hit, at least traditionally with GC (can't speak for Rust. haven't used it), but in most cases it's worth it to free the developer of having to think about memory usage, and allow him to spend more time thinking about other problems or writing code.

However, what you get in C++ is an attempt to get the best of both worlds. It does this, not by having some sort of unifying idea like Rust's ownership thing (again, haven't used), but by adding on features (stuff the developer has to worry about), as if that's going to fix anything. It seems only to be digging a bigger hole for itself.

Collapse
 
aminmansuri profile image
hidden_dude • Edited

The problem isn't with malloc and free (or new and delete in the case of C++).. but with things like implict object creating when returning values the wrong way, or by improperly setting up copy constructors and things like that. Or the need to make destructors virtual if you use inheritance (don't do it? memory leak)

If it only was a matter of new/delete then it would be simple, like C is.
C++ is unwieldy.. too many rules. There are better, more productive languages these days.