Live data from Hacker News

A Usable C++ Dialect That Is Safe Against Memory Corruption

ithare.com

21–30 of 103 posts

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#21
post #15
post #2

This is basically how I program C++. Except that I try to avoid the 'new' keyword too by std::make_unique and std::make_shared. This way there are literally zero 'new' and 'delete' or 'malloc' or 'free' calls in your program.

Likewise, I avoid -> if possible.

Like, the operator ->? Why, and what do you use instead?

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#22

Earlier quoted context omitted.

As far as I'm aware, if you stay within the confines of smart pointers (and don't drop down to the raw pointer it owns) you will never encounter undefined behavior. You may have crashes if you try to double free something, but these are defined to crash rather than letting the compiler optimize out checks.

> As far as I'm aware, if you stay within the confines of smart pointers (and don't drop down to the raw pointer it owns) you will never encounter undefined behavior. Deref'ing an empty (e.g. moved-from) unique_ptr is still UB.

Yeah, there's that. It would be nice if C++ did a emptiness check for you, but I guess that this wasn't in the cards…

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#23
post #2

This is basically how I program C++. Except that I try to avoid the 'new' keyword too by std::make_unique and std::make_shared. This way there are literally zero 'new' and 'delete' or 'malloc' or 'free' calls in your program.

I would try to go further and wrap them in a class to hide the heap usage and expose the valid uses in the interface. Then it reads like a value and walks like one too. Unless I need virtual inheritance I guess.

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#24
Even when you use RAII, const-by-default, shared pointers, type-rich APIs, and the like, you're still using C++. That means you're still tied to C's legacy defaults (of UB) and that also means you're still using C++ value categories. If your "safe" C++ subset uses references, it can't be guaranteed to be safe (since plenty of valid code will lead to UB).

More info in the value category cheat sheet: https://github.com/jeaye/value-category-cheatsheet/blob/mast...

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#26
post #10
post #4

I have a question about this. Articles like http://blog.llvm.org/2011/05/what-every-c-programmer-should-... have convinced me that even if C or C++ reads logically like it is safe, there is a possibility that the compiler can rewrite your code in an acceptable way according to the standards such that the checks that are clearly visible in your code disappear, opening up the very problems that you thought you were pro…

Perhaps someone better versed in those compilers can add to/correct me here, but I'm pretty sure that can only happen if you're invoking UB somewhere along the line.

That's exactly the problem. C/C++ has undefined behavior as part of the language spec, so it can never be safe unless you use a compiler that promises to reject programs that invoke undefined behavior.

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#27
post #6

"Now, we can extend our allocation model with a few additional guidelines, and as long as we’re following these rules/ guidelines, our C++ programs WILL become perfectly safe against memory corruptions." What could possibly go wrong?

If you're not being facetious, then really, not much. You really can't go wrong with smart pointers unless you explicitly try to access the memory it handles rather than going through its normal interface (e.g., not using get()). Shared pointers are basically reference counted just like many other language handle memory management.

> > as long as we’re following these rules/ guidelines

If that's an assumption, it's not worth much. A safe language is one that enforces the rules, not one that hopes the program authors self-enforce.

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#29

I feel like stuff like this is why golang and rust were created. (edit: Forgot about Rust, sorry!)

I feel like stuff like this is why golang was created.

Or more properly, why Rust was created.

I've grudgingly used C++ on some projects because of other constraints such as the target platform. Due to the compiler version, we're stuck on C++11, which is... OK. But keeping straight what we can use, and what we can't, and which kinds of pointers we should be using when is a considerable burden.

Still working through "Effective Modern C++" while learning the ins and outs of it in general.

Re: A Usable C++ Dialect That Is Safe Against Memory Corruption

#30
post #27

Earlier quoted context omitted.

If you're not being facetious, then really, not much. You really can't go wrong with smart pointers unless you explicitly try to access the memory it handles rather than going through its normal interface (e.g., not using get()). Shared pointers are basically reference counted just like many other language handle memory management.

> > as long as we’re following these rules/ guidelines If that's an assumption, it's not worth much. A safe language is one that enforces the rules, not one that hopes the program authors self-enforce.

I don't think anyone claimed the _language_ was safe.
Post reply on HN