I was recently playing around with pointers in fortran, what I wanted to acheive was an array of pointers where each pointed to a different elemnt in an array. In C/C++ this is simple to achieve, something a bit like this for example:
However, this isn’t natively supported in fortran at the moment. This is perhaps with good reason, in fortran by assuming that pointers to an array point to a contiguous part of the array it avoids alaising; meaning the compiler can make curtain assumptions. An example of a pointer in fortran would be:
To acheive the behaviour I’m interested, one method is to declare a derived type, and then make an array of that type. For example, to match the behavour of the earlier C/C++ example you could do:
You might rightly ask, how does this perform? Surely using a derived type and having to invoke a bit more heavy machinery can’t be too performant. Well below is the interesting part of the assembly:
This was compiled with GCC 8.4.0 on an Intel based system. The interesting bit is that, barring some addtional standard setup bits required by fortran, the assembly is exactly the same. So to answer the question, is this apporach performant in fortran; the answer is its as performant as C/C++ in this case.
I also tried this on an Arm based system, but the differences were more significant. But frankly I put this down to the fortran compiler for Arm.