Silverfrost Forums

Welcome to our forums

Possible bug with function that returns a pointer.

6 Apr 2012 8:38 #9962

The following code contains a function list() that returns a pointer to an array of 10 real values containing 1.0, 2.0, ..., 10.0.

Unfortunately, I get an access violation error when I try and run it. It works fine with my other compilers, It is standard Fortran 90.

I do have a workaround by creating a local array and pointing the function result to that before the function returns, then it works ok.

Best regards, David.

module foo
contains
   function list() result(list_vals)
      real, pointer :: list_vals(:)
      integer :: i
      allocate(list_vals(10))
      do i=1, 10
         list_vals(i) = real(i)
      end do
   end function list
end module foo

program anon
   use foo
   real, pointer :: a(:)
   integer :: i
   
   ! Get the list
   a => list()
   
   ! Print the list
   do i=1, 10
      print *, a(i)
   end do
   
   ! Remove list from the heap
   deallocate(a)
end
7 Apr 2012 4:16 #9964

I have logged this as a bug to be investigated.

Please login to reply.