davidb
Joined: 17 Jul 2009 Posts: 560 Location: UK
|
Posted: Fri Apr 06, 2012 9:38 pm Post subject: Possible bug with function that returns a pointer. |
|
|
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.
Code: |
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
|
_________________ Programmer in: Fortran 77/95/2003/2008, C, C++ (& OpenMP), java, Python, Perl |
|