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.
A Usable C++ Dialect That Is Safe Against Memory Corruption
21–30 of 103 posts
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#22Earlier 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.
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#23This 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.
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#24More 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
#25(edit: Forgot about Rust, sorry!)
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#26I 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.
Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#27"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.
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
#28Re: A Usable C++ Dialect That Is Safe Against Memory Corruption
#29I feel like stuff like this is why golang and rust were created. (edit: Forgot about Rust, sorry!)
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
#30Earlier 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.