How do we check if a pointer is NULL pointer?
I always think simply if(p != NULL){..} will do the job. It will.
I always think simply if(p != NULL){..} will do the job. It will.
I have a draft version of the C standard (ISO/IEC 9899:1999), and it has some fun things to say about that call. For starters, it mentions (ยง7.21.1/2) in regards to memcpy that Where an argument declared as size_t n specifies the length of the array for a function, n can have the value zero on … Read more
A message sent to a nil object is perfectly acceptable in Objective-C, it’s treated as a no-op. There is no way to flag it as an error because it’s not an error, in fact it can be a very useful feature of the language. From the docs: Sending Messages to nil In Objective-C, it is … Read more
The two concepts are orthogonal: A void pointer, (void *) is a raw pointer to some memory location. A null pointer is a special pointer that doesn’t point to anything, by definition. It can be a pointer to any type, void or otherwise. A void pointer can be null or not: void *void_ptr1 = nullptr; … Read more
In the description of the finalization process we see (Fortran 2008, 4.5.6.2) If the dynamic type of the entity has a final subroutine whose dummy argument has the same kind type parameters and rank as the entity being finalized, it is called with the entity as an actual argument. Otherwise, if there is an elemental … Read more
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
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
Why does this work? This works because sizeof is a compile time construct, with the exception of variable length arrays is not evaluated at all. If we look at the C99 draft standard section 6.5.3.4 The sizeof operator paragraph 2 says(emphasis mine): […] The size is determined from the type of the operand. The result … Read more
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
You can; the null pointer is implicitly converted into boolean false while non-null pointers are converted into true. From the C++11 standard, section on Boolean Conversions: A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null … Read more