Reputation: 3287
What happens if I allocate a F90 pointer thus:
real, pointer :: abc(:)
allocate abc (nx*ny*nz)
I pass abc to a subroutine, where i redefine it as
real arg1(nx,ny,xz)
This seems to work fine.
But if I redefine as 2D array, I get a segfault.
real arg1(nx,ny)
With reordered arrays as above, it should work. Why does it fail? Any help will be appreciated.
Thanks.
Upvotes: 3
Views: 1505
Reputation: 6918
I don't really understand your problem, what you do should work, since in this case only a reference to the array is passed.
program test
integer :: nx, ny
real, pointer :: abc(:)
nx = 2
ny = 3
allocate(abc(nx**3))
call my_sub(abc, nx, ny)
write(*,*) abc
end program
subroutine my_sub(abc, nx, ny)
integer :: nx, ny
real :: abc(nx,ny)
abc = 1.0
end subroutine
To know how the arrays are passed, you can read this page. At the bottom you can find a table with possible situations. Here the bottom left case applies.
Upvotes: 3
Reputation: 50947
It fails because you're lying to the compiler about the size of the array.
In Fortran 77, people used to use the tricks all the time, because there was no option. In our current enlightened age, we should never do these sorts of tricks -- only Fortran77 and C programmers have to resort to such skulduggery.
As per the answers to changing array dimensions in fortran , if you want to resize an array, or just create a reference to it with a different shape, you can use the RESHAPE
intrinsic, or use an array pointer.
Upvotes: 5