Reply to post:

Boffins debunk study claiming certain languages (cough, C, PHP, JS...) lead to more buggy code than others

DrXym Silver badge

C++11, has unique_ptr / shared_ptr templates in the standard lib which reduce the risk in using raw pointers. But they're not inherent to the language nor is any form of automated memory management. Libs like boost, QT etc have analogous wrappers, e.g. boost::scoped_ptr, QSharedPointer. But this is still an opt-in to safety, not safe by default.

The language itself has new and delete operators but they must be called correctly. Omit the delete, and it leaks. Delete an invalid pointer and the heap corrupts. Delete an array without the [], and only the first element's destructor is called. Call an invalid pointer and it crashes, or worse, doesn't.

It's certainly advisable to use smart pointer classes where possible but they are still not mandatory.

And screwups are still possible. I reviewed some code that assigned on shared pointer to another. The shared ptr on the left was typedef'd to a base class, the one on the right was typedef'd to a child class. Since the typedefs were not the same, the right hand side implicitly fed its inner raw pointer into the constructor of the left hand side and both smart pointers came away with a reference of 1. The first var out of scope deleted the object and left the second looking at garbage.

The point being none of this is even an issue in Rust. The compiler will insert the memory allocation / deallocation for you and will kick your butt if you violate object lifetimes rules. The result is software that doesn't suffer from an entire category of common programming fault.

POST COMMENT House rules

Not a member of The Register? Create a new account here.

  • Enter your comment

  • Add an icon

Anonymous cowards cannot choose their icon

Biting the hand that feeds IT © 1998–2019