Assume I want to create an array that contains a chunk of another array and I want to use a pointer to avoid copying data: if my function returns null
, I get a segfault with gfortran, while ifort and ifx both work. What does the standard say about this edge case? My understanding is that it is illegal to feed a null pointer to another routine, but the intrinsic functions should work?
program test_null
integer, allocatable :: i(:)
integer, pointer :: p(:)
i = [1,2,3,4,5,6,7,8,9,10]
! Returns actual chunk
print *, count(mod(point_to_i(i,2,5),2)==0)
! Segfault: null pointer in intrinsic
print *, count(mod(point_to_i(i,20,5),2)==0)
contains
function point_to_i(i,i1,i2) result(ptr)
integer, intent(in), target :: i(:)
integer, intent(in) :: i1,i2
integer, pointer :: ptr(:)
if (i2>=i1 .and. min(i1,i2)>0 .and. max(i1,i2)<=size(i)) then
ptr => i(i1:i2)
else
nullify(ptr)
endif
end function point_to_i
end program
An alternative would be to allocate the pointer to a 0-size array:
allocate(ptr(0))
but I’m not sure that would lead to a memory leak?