Pointers / Free Literature on Fortran

Pointers can be very handy, but they have to be treated with caution!
Here is an use case in which using pointers I managed to have a compact user interface for string to number conversion yet maintaining the performance of the code. The two main reasons for using the pointer interface were that (1) in the str2num conversion using indexing with integer(1) values helped in the performance, (2) stream lining through a long chain can be easily done by simply shifting the pointer instead of juggling with indexes.

I also agree with @FortranFan, I do prefer using procedure pointers rather than EXTERNAL procedures.

It is not a black-&-white matter, more of a “it depends”, and one should always ponder between how many lines of code are saved against the possible pitfalls of mangling with pointers.

You can for instance manage flexible type bound data containers with pointers:

type :: mytype
  logical :: imowner = .false.
  real, pointer :: x(:) => null()
end type

Will enable you to either allocate the data in ‘x’ or just use it as temporal working pointer. One has to be careful when destroying the object on whether one did actually allocate it or just point to something else. If this is done properly, the end user interfaces can be really clean and powerful.

1 Like