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.

2
  • @SethCarnegie : We get (and answer) about 2 a day here on SO... Commented Feb 13, 2012 at 20:16
  • Thank you for your answers. The error turned out to be entirely unrelated to the array redefinition issue. Commented Feb 13, 2012 at 21:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your answer. The error was simply elsewhere, but I now know how the array redefinitions work. Thanks.
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.

2 Comments

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.
@steabert: Thanks for your answer. Since I am using icc as well, the link was a great read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.