What is the difference between
procedure(some_routine), pointer :: ptr
ptr => null()
and
procedure(some_routine), pointer :: ptr
nullify(ptr)
Does nullify do something behind the scenes? Or is it just two different ways of doing the same thing?
The null pointer is basically used in a program to assign the value 0 to a pointer variable of any data type. The void pointer, on the other hand, has no value assigned to it and we use it to store the addresses of other variables in the program- irrespective of their data types.
A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to any pointer that points to a valid object. However, depending on the language and implementation, an uninitialized pointer may not have any such guarantee.
A void is nothing but takes up space; null is nothing at all. In other words, you could measure a void but null offers nothing to measure. 4.1 Void is used to indicate that a function/method does not return any data type. Null indicates that a pointer variable is not pointing to any address.
A NULL pointer dereference occurs when the application dereferences a pointer that it expects to be valid, but is NULL, typically causing a crash or exit. NULL pointer dereference issues can occur through a number of flaws, including race conditions, and simple programming omissions.
The result is completely identical. The assignment sign =>
can be also used for variable initialization or derived type components default initialization, where the nullify
statement is unusable, but that is only a syntactic thing, it is not a proper assignment in fact.
For example
type t
real, pointer :: ptr => null()
end type
is the default initialization of a component, and
program p
real, pointer :: ptr2 => null()
is initialization of a variable. The variable ptr2
is implicitly save
as any other initialized variable (a common source of errors).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With