3

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.

Jonathan Dursi
  • 50,107
  • 9
  • 127
  • 158
SkypeMeSM
  • 3,197
  • 8
  • 43
  • 61

2 Answers2

5

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.

Community
  • 1
  • 1
Jonathan Dursi
  • 50,107
  • 9
  • 127
  • 158
  • Thank you for your answer. The error was simply elsewhere, but I now know how the array redefinitions work. Thanks. – SkypeMeSM Feb 13 '12 at 21:49
3

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.

steabert
  • 6,540
  • 2
  • 26
  • 32
  • 2
    That's how those arrays are passed for the intel compiler, and indeed those are pretty likely implementations; but the standard doesn't guarantee anything about this; if a compiler vendor thinks its advantageous under some circumstances to (say) pass small arrays copy in/copy out on the stack, it's free to do that. – Jonathan Dursi Feb 13 '12 at 20:48
  • @steabert: Thanks for your answer. Since I am using icc as well, the link was a great read. – SkypeMeSM Feb 13 '12 at 21:47