2

The following code gives segmentation error when compiled with pgf90 on the Linux system, while is run successfully when I used the Intel Visual FORTRAN on Windows.

program main 
implicit none 
integer:: a(3), b(3) ,c(3)
a=[3, 4, 5]
b=[1, 2, 3]
call sub(a,b,c)
write(*,*)'a+b = ',c 
end program main

subroutine sub(a,b,c) 
implicit none 
integer, intent(in)::a(:),b(:)
integer, intent(out)::c(:)
c=a+b
end subroutine sub 

Any explanation for this ?

3
  • shouldn't it be integer:: a(3), b(3) ,c(6) ? Commented Feb 24, 2012 at 23:00
  • Where does the segfault occure? Could you use a module? Commented Feb 24, 2012 at 23:04
  • @JulienMay: I don't think so. Adding two arrays of size 3 returns an array of size 3, not size 6. Commented Feb 25, 2012 at 0:13

2 Answers 2

4

When you call a subroutine which has assumed shape dummy arguments (as is the case in this program), an explicit interface is required. The easiest way to achieve this, is to put the subroutine in a module, and use the module in the main program.

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

5 Comments

Thanks Julien May, you are right it works when i used integer:: a(3), b(3) ,c(6) And @eriktous currently I used the module method to avoid the segmentation fault but, what you suggest is the good way to do this (module or interface) ?
See this similar question for an example: stackoverflow.com/questions/9374691/array-and-pointer-shapes
Thanks @M.S.B. but do you suggest between interface and module option ?. As currently i have used the module, so is there any need to use the interface ?
@zahoor: Don't confuse an explicit interface with an interface block. An interface block can be written to provide an explicit interface, but using a module also provides an explicit interface for all (public) procedures in that module.
A module automatically informs other procedures of the same module and entities that "use" the module of the interface. You could write an interface block to describe the interface, but then you have an extra item to maintain. If you change the procedure, you also have to change the interface block -- extra work and opportunity for bugs. There are reasons for using interface blocks, such as describing routines for which you don't have the Fortran source code, e.g., provided already compiled, or written in another language.
1

It might be helpful to use standard Fortran 90 syntax, specifically in how you declare and initialize arrays.

 program main 
 implicit none 
 integer, dimension(3):: a, b ,c
 a=(/3, 4, 5/)
 b=(/1, 2, 3 /)
 call sub(a,b,c)
 write(*,*)'a+b = ',c 
 end program main

Comments

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.