Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by saagarjha on Thursday March 15, 2018 @03:02PM
(#16595608)
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.
[ Parent
| on HN ]
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by masklinn on Thursday March 15, 2018 @03:12PM
(#16595700)
> 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
by saagarjha on Thursday March 15, 2018 @03:22PM
(#16595782)
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
by EpicEng on Thursday March 15, 2018 @04:44PM
(#16596392)
Then you have a check on every deref, which means you incur a relatively large performance penalty for using a smart pointer. If you want stuff like this there are other languages out there.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by mcguire on Thursday March 15, 2018 @06:10PM
(#16596920)
Would it be possible to write the check so that the null test overlaps with the rest of the instructions? If the test is anyways assumed to pass, you should only get a 1-instruction overhead, right?
(That'll only work for null.)
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by kllrnohj on Thursday March 15, 2018 @09:27PM
(#16597921)
1 instruction != 1 clock cycle. In particular that would utterly kill the usage of unique_ptr on things like small embedded processors or microcontrollers that either lack speculative execution entirely or do not have the level of branch predictor & speculative execution capabilities of a high-end x86 or ARMv8 CPU.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by nobugs on Friday March 16, 2018 @04:42AM
(#16599379)
FWIW: in general, simpler controllers (especially those in-order ones) tend to be much more friendly to branching (exactly because they're not out-of-order). NB: I am not arguing whether unique_ptr<> should check or not: if I want checked version, I will write my own wrapper, it is not a rocket science.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by RossBencina on Friday March 16, 2018 @12:05AM
(#16598543)
While we're on "nice to haves," it would be great if there was a way to explicitly terminate the scope of objects. Then you could have an atomic move-from-and-remove-from-scope operation. Which removes any possibility of accessing the moved-from object again.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by staticassertion on Thursday March 15, 2018 @04:06PM
(#16596125)
> 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.
No. Even with smart pointers it's possible (move out of unique_ptr, deref). But even with no pointers it's possible - index into an array without checking the bound, signed int overflow, etc.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by yoklov on Thursday March 15, 2018 @05:07PM
(#16596527)
UAF is still possible. Iterator invalidation, and such.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by zach43 on Thursday March 15, 2018 @03:09PM
(#16595667)
Wouldn't you have a problem with reference cycles in C++ smart pointers? Not sure if they do anything special to prevent this.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by nobugs on Friday March 16, 2018 @01:01AM
(#16598744)
std::unique_ptr<> won't allow you to have 'owning' reference cycles; neither 'owning' reference cycles are really necessary in real-world programs.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by saagarjha on Thursday March 15, 2018 @03:15PM
(#16595724)
std::weak_ptr is there for resolving reference cycles.
Re:A Usable C++ Dialect That Is Safe Against Memory Corruption
by pdpi on Thursday March 15, 2018 @08:54PM
(#16597797)
Which means you have to design around it. You can definitely run into problems with cyclical shared_ptr dependencies