New Reply

FORTRAN-Passing array to subroutine-segmentation fault

 
Share Thread
Jun6-12, 05:25 AM   #1
 

FORTRAN-Passing array to subroutine-segmentation fault


Hi all !!

I am new to fortran. Please see the following code. It is a simple code to pass an array to a subroutine and print it, but doesnt behave that way:

Code:
    program exp_realloc
    implicit none

    integer,allocatable,dimension(:,:):: array
    integer::i,j

    allocate(array(3,3))

    write(*,*)size(array,1)," ",size(array,2)

    do i=1,3
        do j=1,3
            array(i,j)=i*j
            write(*,*)array(i,j)
        enddo
        write(*,*)
    enddo

    CALL func(array)

    end program exp_realloc


    subroutine func(array)

    integer,dimension(:,:),intent(in)::array
    integer::i,j

    write(*,*)"********* Inside subroutine **********"
    write(*,*)size(array,1)," ",size(array,2)

    write(*,*)array(1,1)
    write(*,*)array(1,2)
    write(*,*)array(2,1)
    do i=1,3
        do j=1,3
            write(*,*)array(i,j)
        enddo
    enddo

    end subroutine func
The output is:
Code:
    3 3
    1
    2
    3
    2
    4
    6
    3
    6
    9
    ********* Inside subroutine **********
    131097 1
    Segmentation fault
Two questions:

1. Why is the size of array not being printed correctly in the subroutine?
2. Why the segmentation fault? Even array(1,1) cant be accessed?

Thank you in advance !!
PhysOrg.com science news on PhysOrg.com

>> City-life changes blackbird personalities, study shows
>> Origins of 'The Hoff' crab revealed (w/ Video)
>> Older males make better fathers: Mature male beetles work harder, care less about female infidelity
Jun6-12, 06:33 AM   #2
 
I'm going to play with this for a minute. Just letting you know someone is looking and doing something.

Alright, done playing. I split your program up into 2 pieces (just preference). I changed the way you allocate your array by defining a parameter named dim (=3) as you can see at the top of main. That way, you don't have these "magic numbers" all around and can easily change loops if you want. When you pass your array to the subroutine, you need to also pass it's dimension (someone correct me if that isn't exactly right; it works). Define the array's dimension and self as an integer with those dimensions. Reformatted a bit. Compiled with

Code:
gfortran shitij_main.f90 shitij_sub.f90
./a.exe
Let me know if you need anything else.

Cheers.

Code:
      program shitij_main
      implicit none

      integer :: i
      integer :: j
      integer, parameter :: dim = 3
      integer, dimension(dim,dim) :: array

      write(*,*) size(array,1), " ", size(array,2)

      do 10 i = 1,dim

         do 20 j = 1,dim

            array(i,j) = i*j
            write(*,*) array(i,j)

 20      continue         

         write(*,*)

 10   continue

      call shitij_sub(array,dim)

      stop
      return

      end program shitij_main
Code:
      subroutine shitij_sub(array,dim)

      integer :: i
      integer :: j
      integer :: dim
      integer, dimension(dim,dim) :: array

      write(*,*) "********* Inside subroutine **********"
      write(*,*) size(array,1), " ", size(array,2)

      write(*,*) array(1,1)
      write(*,*) array(1,2)
      write(*,*) array(2,1)

      do i = 1,dim
         do j = 1,dim
            write(*,*) array(i,j)
         enddo
      enddo

      end subroutine shitij_sub
Jun6-12, 02:05 PM   #3

Math 2012
 
Recognitions:
Science Advisor Science Advisor
Swartizm''s work-round works, but if you really want to write the routine as
Code:
subroutine func(array)
    integer,dimension(:,:),intent(in)::array
... 
end
you have to do one of two things.

Either write an interface defintion for the subroutine, so the compiler "knows" you are doing this when it complies the CALL FUNC statement.

Or, make the subroutine part of a module, and you then get the interface definition. generated for free.

If you don't do that, the subroutine will get "random" information about the size of the array. That's why the dimensions you printed in the subroutine were wrong, and when it tried to access the elements of the array using those wrong dimensions, the program crashed trying to access part of the computer's memory that didn't belong to your program.

See "Danger of calling Fortran 90 style routines" in http://www.cs.rpi.edu/~szymansk/OOF90/bugs.html
Jun6-12, 08:32 PM   #4
 

FORTRAN-Passing array to subroutine-segmentation fault


O.k., one more piece of information...

...it has always been the case that when you pass a matrix to a subroutine, the subroutine needs to know (ahead of time or passed in at call time) the size of all the dimensions but the last one. You can pass or know the size of the last dimension, too, but it is not a must.

This has to do, of course, with the information the program need to access the matrix memory addresses.
Jun9-12, 12:51 AM   #5
 
Thanks a lot guys !!

This is just a toy program, in the actual program I have to (re)allocate inside the subroutine, so I cant pass the dimensions (because I don't know what they will be). The array is allocatable. I needed to make a (somewhat) realloc equivalent of C in FORTRAN.
I just made an interface and put it inside the program, as AlefZero said and it works !! And the link is useful too ! Thanks !
And thank you so much swartzism for the prompt reply !

And why are fortran errors so weird? If I miss an endif or a then after an if, it shows me an error in some unrelated loop....here also, it should have just told me at compile time that I needed to give it some sort of prototype for the subroutine before using it. :|
Jun9-12, 08:24 AM   #6
 
Hhhmmm....I don't know about Fortran error message being weird. I have always found them pretty good...often time they even quote your line of code and another line below it showing with a caret exactly where the problem is.

Missing then? endif? Of course the problem is going to be reported somewhere else...try leaving a ' } ' out in C ....sooner or later and error will be reported and could very well be at the very end of the program when, finally, the brace count did not come down back to zero.

Unlike C, where all "sub-modules" are functions and require prototype, in Fortran we have functions and subroutines, typically, subroutines do not need a prototype. I am just making this comment but saying no more since I don't have experience with interfaces.

Anyway, just more commentary.
New Reply

Similar discussions for: FORTRAN-Passing array to subroutine-segmentation fault
Thread Forum Replies
Fortran - Allocate array in subroutine Programming & Comp Sci 6
Fortran Segmentation Fault Programming & Comp Sci 5
Fortran Segmentation fault Programming & Comp Sci 5
Fortran segmentation fault Engineering, Comp Sci, & Technology Homework 15
Fortran, subroutine with allocatable, intent(out) array Programming & Comp Sci 2