the program is quite simple :
!
subroutine print_array(a)
integer, dimension(*) :: a
print*,'print::loc(a)=',loc(a)
end subroutine print_array
!
program test
integer, target, dimension(10) :: ival
integer, pointer, dimension(:) :: ival_ptr
!
print*,'test::loc(ival)=',loc(ival)
!
ival_ptr=>ival
call print_array(ival_ptr)
!
end
Version 1 - Contiguous memory -
gfortran -fcheck=array-temps -Warray-temporaries main.F
main.F:12.23:
call print_array(ival_ptr)
1
Warning: Creating array temporary at (1)
And during execution, I can see that there is no copy:
./a.out
test::loc(ival)= 140731006653952
print::loc(a)= 140731006653952
Version 2 - Non contiguous memory -
Just replace
call print_array(ival_ptr)
by:
call print_array(ival_ptr(1:10:2))
gfortran -fcheck=array-temps -Warray-temporaries main.F
main.F:13.23:
call print_array(ival_ptr(1:10:2))
1
Warning: Creating array temporary at (1)
and a warning during execution:
./a.out
test::loc(ival)= 140733975879936
At line 13 of file main.F
Fortran runtime warning: An array temporary was created
print::loc(a)= 25096048
Analysis:
- version 1 :
- print_array receive the array ‘a’ as contiguous memory block
- we see that both arraies have the same adress : loc(a) = loc(ival)
- no runtime warning
- version 2 :
- print-array receive a temporary array
- loc(a) != loc(ival)
- runtime warning
Questions :
- Version 2 I get the expected result, a warning during compilation and another at runtime
- Version 1 : I don’t understand the warning during compilation ?, is there a risk to pass a pointer as argument to a subroutine ?
What do you think ?
Thanks.