INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

bogus segmentation invalid memory reference from gfortran

bogus segmentation invalid memory reference from gfortran

bogus segmentation invalid memory reference from gfortran

(OP)
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 !

RE: bogus segmentation invalid memory reference from gfortran

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 

RE: bogus segmentation invalid memory reference from gfortran

Quote (zmth)


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 ...

RE: bogus segmentation invalid memory reference from gfortran

(OP)
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 ?

RE: bogus segmentation invalid memory reference from gfortran

Quote:


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
call rr(m,n)
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.

RE: bogus segmentation invalid memory reference from gfortran

(OP)
"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

RE: bogus segmentation invalid memory reference from gfortran

Quote (zmth)


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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close