Uninitialized pointers in code

int* ptr = NULL; //Is this going to avoid the problem This will cause ptr to point to NULL which you can explicitly check for as a default/uninitialized value. It prevents the problem you describe, but a careless programmer can still accidentally dereference a null pointer without checking, causing undefined behaviour. The main advantage is … Read more

Does not evaluating the expression to which sizeof is applied make it legal to dereference a null or invalid pointer inside sizeof in C++?

I believe this is currently underspecified in the standard, like many issues such as What is the value category of the operands of C++ operators when unspecified?. I don’t think it was intentional, like hvd points outs it is probably obvious to the committee. In this specific case I think we have the evidence to … Read more

What is the overhead of Rust’s Option type?

Yes, there is some compiler magic that optimises Option<ptr> to a single pointer (most of the time). use std::mem::size_of; macro_rules! show_size { (header) => ( println!(“{:<22} {:>4} {}”, “Type”, “T”, “Option<T>”); ); ($t:ty) => ( println!(“{:<22} {:4} {:4}”, stringify!($t), size_of::<$t>(), size_of::<Option<$t>>()) ) } fn main() { show_size!(header); show_size!(i32); show_size!(&i32); show_size!(Box<i32>); show_size!(&[i32]); show_size!(Vec<i32>); show_size!(Result<(), Box<i32>>); } … Read more