Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bogus segmentation invalid memory reference from gfortran

Status
Not open for further replies.

zmth

Technical User
Aug 5, 2015
57
PH
Has anyone had this problem with the gfortran compiler

call rr(1,1)
end
recursive subroutine rr(j,k)
if(j>3) then
print*,k
else
k=k*j
call rr(j+1,k)
endif
end

Clearly everything is well defined and should not be getting any run time errors such as 'segmentation... invalid memory..'
which it is giving !
 
Hi zmth,

The problem is, that you modified procedure parameters k, j in the procedure.

I changed it so:

zmth_05.f95
Code:
call rr(1,1)
end

recursive subroutine rr(j,k)
  if(j>3) then
    print*,k
  else
    ! k=k*j
    call rr(j+1,k*j)
  endif
end

and now it works and delivers result 6 :
Code:
mikrom@mikrom-Lenovo-S500 ~/Work/fortran $ gfortran zmth_05.f95 -o zmth_05
mikrom@mikrom-Lenovo-S500 ~/Work/fortran $ ./zmth_05
           6
 
zmth said:
Clearly everything is well defined and should not be getting any run time errors
Sometimes the things are not so clearly clear, but slightly different ...
 
I find the following also works:

call rr(1,1)
end
recursive subroutine rr(j,k)
if(j>3) then
print*,k
else
m=k*j
n=j+1
call rr(n,m)
endif
end

That is for any integer symbols m,n as long as they are not k nor j. The question is why should it matter if i reuse either or both of the same symbols j or k in either place ? But if do get segmentation error. Wonder if anyone NOT using gfortran compiler gets error if reuse same symbols j or k ?
 
The question is why should it matter if i reuse either or both of the same symbols j or k in either place ? ...
When you use in the main program instead of constants rather variables as arguments of the subroutine, then your approach will work - see below.

zmth_051.f95
Code:
m=1
n=1
[highlight]call rr(m,n)[/highlight]
end

recursive subroutine rr(j,k)
  if(j>3) then
    print*,k
  else
    k=k*j
    call rr(j+1,k)
  endif
end

Code:
mikrom@mikrom-Lenovo-S500 ~/Work/fortran $ gfortran zmth_051.f95 -o zmth_051
mikrom@mikrom-Lenovo-S500 ~/Work/fortran $ ./zmth_051
           6

It's obvious, that the compiler behaves different when the subroutine arguments are variables and when constants.
 
When you use in the main program instead of constants rather variables as arguments of the subroutine, then your approach will work"

That is interesting also. Then again I ask the same question to the logic as to why that is necessary and if anyone has the same problem of not getting the correct final result of 6 or error on any other compiler other than gfortran using:

call rr(1,1)
end
recursive subroutine rr(j,k)
if(j>3) then
print*,k
else
k=k*j
j=j+1
call rr(j,k)
endif
end
 
zmth said:
Then again I ask the same question to the logic as to why that is necessary and if anyone has the same problem of not getting the correct final result of 6 or error on any other compiler other than gfortran
Everything is very logical.
By default, the subroutine parameters are passed by reference.
So, when you have declared your subroutine as rr(j,k) and call it with variables as parameters, i.e. call rr(m,n), then the argument k refers to the variable n and you can modify it.
But when you call the subroutine with constants as parameters, i.e. call rr(1,1), then k refers to the constant 1. Constants could not be modified - so if you try it, you will get an error.

But also if you would use the variables as subroutine parameters, using subroutine arguments for temporary computation would be very bad practice. What should be the purpose ?

You are still asking about any other compiler than gfortran...
Currently, I have only gfortran. IMO it's very good standard fortran compiler and I doubt that any other compiler behave in this case otherwise.

But you can try any other compiler self:
There is other good free compiler I used some years ago: g95.
Then, there are some commercial compilers - one of the best shoud be intel fortran compiler

You can try them and post here an information, if they behave in this case otherwise than gfortran.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top