I’m trying to fully understand pointers in Fortran and what it’s the exact behaviour of them. I’m reading “Modern Fortran explained (2018)” and also going through https://www.tutorialspoint.com/fortran/fortran_pointers.htm but they’re still unclear to me.
I thought that pointers are just objects that save the memory address of an actual variable when associated to it. That is, if
point is this a pointer object and
targ is a target variable then
point => targ makes that when using
point in any calculation, print or whatever operation in the program it will exactly behave as
targ (and with behave I simply mean that it will just use the value of
targ but now using
point, of course assuming both have same data type). Following this reasoning my expectation will be that
point is linked to
targ and it’s numerical value will be associated always with
targ, so I I change the value of
targ when I use
point I will get the same value as in
targ. What I will not expect is that if I change
point it will also change the value of
targ. Let me use the following example (basically the same as in the link above).
What I understand:
program pointers implicit none integer, pointer :: point integer, target :: targ targ = 3 point => targ print'(" point = ", g0)', point print'(" targ = ", g0)', targ end program pointers
point = 3 targ = 3
Even though this makes sense to me, what it’s actually happening here? Is
pointonly getting the memory address of
targor it is actually storing the numerical value of
targthus duplicating memory usage? I think is the former but not sure.
What I don’t understand:
Example 2: Same as Example 1 but doing:
program pointers implicit none integer, pointer :: point integer, target :: targ point => targ point = 3 print'(" point = ", g0)', point print'(" targ = ", g0)', targ end program pointers
point = 3 targ = 3
Here is when I start to not understand. Why
targis getting the value of
point? In principle I will expect that, although
pointis linked to
targ, changing the value of
pointwill not alter
targ's. What is happening here? Actually, following this weird thought that I have about what a pointer does, what does it means to use the operation
=with a pointer? I believe that using this operation there’s a real assignment of the value
3to the variable
pointand that now there’s an actual variable
pointoccupying the needed memory of a default integer storing the value
Example 3: although I think it is enough for the moment, maybe another fundamental thing that I don’t understand. What is the meaning of using
allocateover a pointer in general, but in particular to a scalar variable (either integer or real)? In the book I’m reading mentioned above it says “[referring to a pointer] … or may be given fresh storage using the
allocatestatement…”. What does it means to give fresh storage? And particularly to a scalar pointer as for example
allocate (point)being point of type integer/real?
Sorry for being so verbose when I think my question is clear with just the second paragraph.
Thanks in advance!
PS. Is there a way to keep track or get logs of the actually memory usage of variables and pointers and see the values and links of them?