Can‘t call a subroutine name in a subroutine in Fortran90

Click For Summary

Discussion Overview

The discussion revolves around a Fortran90 programming issue related to calling subroutines within other subroutines. Participants explore the behavior of the code when attempting to call a subroutine defined within a 'contains' block and the resulting segmentation fault during execution.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant provides a code example and describes the issue of a segmentation fault occurring when calling the subroutine 'fcn' within 'CaoB', which is intended to call 'B'.
  • Another participant questions how the main program determines which subroutine to call, seeking clarification on the interface and external declarations.
  • A participant suggests that adding 'external B' or 'external fcn' does not resolve the issue, indicating confusion about the interface mechanism.
  • One participant confirms that the code appears to work correctly on different Fortran compilers, suggesting that the problem may be environment-specific.
  • Another participant expresses uncertainty about their environment, noting a potential issue with their version of ifort and mentioning that problems are often resolved with updates.
  • A later reply identifies a workaround by moving the subroutine 'B' outside of the 'contains' block, which resolves the segmentation fault, indicating a structural issue in the code organization.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the segmentation fault, with some suggesting it may be related to the code structure while others believe it could be environment-related. No consensus is reached on the underlying issue.

Contextual Notes

The discussion highlights limitations in understanding how the Fortran90 interface works in this context, particularly regarding the placement of subroutines and the implications for calling conventions.

pilipili2021
Messages
5
Reaction score
0
Homework Statement
Coding by ifort
Try to call a uncertain name subroutine
Relevant Equations
NONE
Here is the code for example:
Fortran:
PROGRAM  main
  CALL B()
  CALL CaoB(B)
  
  contains
   
  subroutine B()
  IMPLICIT NONE
  write (*,*) 'fk'
  end subroutine B

 subroutine CaoB(fcn)
 implicit none

 INTERFACE
   SUBROUTINE fcn()
     IMPLICIT NONE
   END SUBROUTINE fcn
 END INTERFACE

 write (*,*) '1'
 call B() 
 write (*,*) '2'
 call fcn()
 write (*,*) '3'

 end subroutine CaoB 

END
Then I compile it.

ifort main.F90

It seems no error. Then run it.

./a.outIt show that

fk
1
fk
2
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
a.out 000000000040492A Unknown Unknown Unknown
libpthread-2.31.s 00007F15A65153C0 Unknown Unknown Unknown
Unknown 00007FFFC18B82C0 Unknown Unknown Unknown

Surely there is something wrong when I call the subroutine fcn, which it should be named as B.

So, what should I do? what's wrong? Please let me know if any of you have any ideas or any comments.

thank you
 
Physics news on Phys.org
pilipili2021 said:
which it should be named as B
How does the main program know that ?
[edit] Or rather: the calling program, in this case CaoB

##\ ##
 
Last edited:
BvU said:
How does the main program know that ?

##\ ##
Thank you for your reply.

Do you mean add some code like 'external B' or 'external fcn'? That not works.

In the subroutine 'CaoB' it have a subroutine name 'fcn' determined by the interface. When I call 'CaoB(B)', 'B' as a subroutine name should be instead of 'fcn' in the subroutine 'CaoB'. It should be, but something goes wrong.
 
Your program appears to be fine. I have checked both with GNU Fortran (GCC) 10.2.0 and ifort version 2021.2.0 20210228.
 
Thank you for your reply!
It's a great help.
May be my environment have some problems.
my ifort version is 2021.4.0

So confused.
 
pilipili2021 said:
Thank you for your reply!
It's a great help.
May be my environment have some problems.
my ifort version is 2021.4.0

So confused.
Problems are usually fixed with newer versions :smile:.

By the way, my ifort is on Linux.
 
DrClaude said:
Problems are usually fixed with newer versions :smile:.

By the way, my ifort is on Linux.
My ifort is on ubuntu 20.04 , also on Linux.
Everything worked well before my hard disk suddenly broken.
May problems created with newer versions:cry:
 
finally I find the problem.

If I code like

Call CaoB()
contains
subroutine B()

the error comes.

If I move subroutine B out of 'contains' like

subroutine B()

subroutine something
call CaoB()
end subroutine something

Then the problem is solved.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
8K
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 16 ·
Replies
16
Views
4K