Re:Null Pointer Dereferencing Causes Undefined Behavior
by amelius on Monday April 20, 2015 @05:28PM
(#9410977)
How does Rust deal with this type of situation?
[ Parent
| on HN ]
Re:Null Pointer Dereferencing Causes Undefined Behavior
by lmm on Monday April 20, 2015 @05:33PM
(#9411000)
By not having null pointers. If you want to have a pointer that might not point to anything valid, you do that explicitly with an option type. Of course if you really need to use a possibly-invalid pointer for performance you can drop into unsafe mode.
(At least, the above is what I'd expect from a language like Rust)
Re:Null Pointer Dereferencing Causes Undefined Behavior
by yonran on Monday April 20, 2015 @07:15PM
(#9411402)
But when you do need unsafe code (e.g. for defining a structure that interacts with a C API), is behavior undefined? I’m just looking at a bit of my own offsetof calculation and hoping it won’t break once I turn optimizations on.
unsafe extern "stdcall" fn callback(arg0: *const IUnknown) {
let myself: &MyStruct = &*(arg0.offset(-(&((*std::ptr::null::<MyStruct>()).as_event_handler) as isize)) as *const MyStruct);
}
Re:Null Pointer Dereferencing Causes Undefined Behavior
by dbaupp on Monday April 20, 2015 @08:55PM
(#9411740)
Yes, it generally is. Rust has similar (but not the same) undefined behaviours to C/C++, the compiler just ensures that they can't happen in safe code.
Re:Null Pointer Dereferencing Causes Undefined Behavior
by jerven on Monday April 20, 2015 @07:10PM
(#9411388)
Rust does not have undefined behaviours in the sense of C. C and C++ are rather unique in the very large amount of undefined behaviour that is in common use. A nice comment pcwalton https://news.ycombinator.com/item?id=7601944 gives a better explanation than I can.
The undefined behaviour in C is in my opinion a bad design that unfortunately poisoned a whole lot of software for minor gains in speed somewhere. A mistake that did not need to have become popular but for chance in history.
Re:Null Pointer Dereferencing Causes Undefined Behavior
by alricb on Monday April 20, 2015 @07:07PM
(#9411377)
Currently, by having a single implementation, so that there isn't really a "Rust standard" besides what this or that version of the Rust compiler does and what the documentation says, and if there's a discrepancy, it's a documentation or compiler bug.
There might one day be a "Rust standard", but then let's all wish the standardization process leads to something like the Ada Language Reference Manual instead of the mess that is ANSI C.