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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to pass an allocatable Array to a subroutine?

Status
Not open for further replies.

torstenSchrobe

Technical User
Jul 2, 2009
4
DE
Hi Guys,
I'm a beginner in fortran programming and don't speak english so well, but i hope you will undersand my question ;)

In the program there is an allocatable array, which shall be allocated in the subroutine, and then given back to the main program (beacause in the subroutine i read in the data and get to know how big the array must become). When i try to allocate the array in the subroutine i get a "Segmentation fault".
What can i do to avoid this? Maybe with a pointer on the array?

Please help me! :)

Here is the simplified example:


double precision,allocatable,dimension:)):: array
call readData(array)

subroutine readData(array)
double precision,allocatable,dimension:)):: array
allocate(array(2))
end subroutine readData
 
sry, but i don't understand this other thread....can you give me a solution right to my example?
 
Which version of Fortran are you using? 90, 95 or 2003. It is possible in 95 and 2003 but not in 90. In 90, the caller has to do the allocation.
 
Hallo Torsten,

Here I have more accurate example for you
read_subr.f90
Code:
[COLOR=#a020f0]program[/color] read_subr
[COLOR=#0000ff]! ********************** Main program *********************************[/color]
[COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
[COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color] (:) :: x
[COLOR=#0000ff]! Read Data[/color]
[COLOR=#a020f0]call[/color] read_data([COLOR=#ff00ff]'inpt.dat'[/color], x)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]

[COLOR=#0000ff]! Print Data[/color]
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Array of X-values: '[/color]
[COLOR=#a020f0]call[/color] print_array([COLOR=#ff00ff]'X'[/color], x)
[COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color])

[COLOR=#a020f0]contains[/color]
[COLOR=#0000ff]! ********************** Functions/Procedures *************************[/color]
[COLOR=#a020f0]subroutine[/color] read_data(fname, array)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]character[/b][/color]([COLOR=#804040][b]*[/b][/color]), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: fname
  [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color] [COLOR=#2e8b57][b]out[/b][/color]) :: array
  [COLOR=#2e8b57][b]integer[/b][/color] :: i, nr_lines, nr_elements, stat
  [COLOR=#2e8b57][b]double precision[/b][/color] :: dummy

  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]"Reading the Data from file '"[/color], fname, [COLOR=#ff00ff]"' :"[/color] 
  [COLOR=#804040][b]open[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]file[/b][/color][COLOR=#804040][b]=[/b][/color]fname,[COLOR=#804040][b]status[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'old'[/color],[COLOR=#804040][b]iostat[/b][/color][COLOR=#804040][b]=[/b][/color]stat)
  [COLOR=#804040][b]if[/b][/color] (stat [COLOR=#804040][b].ne.[/b][/color] [COLOR=#ff00ff]0[/color]) [COLOR=#804040][b]then[/b][/color]
    [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) fname, [COLOR=#ff00ff]' cannot be opened !'[/color]
    [COLOR=#804040][b]go to[/b][/color] [COLOR=#ff00ff]20[/color]   
  [COLOR=#804040][b]end if[/b][/color]

  [COLOR=#0000ff]! count number of lines in the file[/color]
  nr_lines [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]0[/color]
  [COLOR=#804040][b]do[/b][/color]
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]end[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]10[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) dummy
    nr_lines [COLOR=#804040][b]=[/b][/color] nr_lines [COLOR=#804040][b]+[/b][/color] [COLOR=#ff00ff]1[/color]
  [COLOR=#804040][b]end do[/b][/color]

  [COLOR=#0000ff]! rewind back to the beginning of the file for unit=1[/color]
  [COLOR=#6a5acd]10[/color] [COLOR=#804040][b]rewind[/b][/color]([COLOR=#ff00ff]1[/color])
  [COLOR=#0000ff]! number of array elements = number of data lines in the file[/color]
  nr_elements[COLOR=#804040][b]=[/b][/color]nr_lines
  [COLOR=#0000ff]! allocate the arrays of given size [/color]
  [COLOR=#804040][b]allocate[/b][/color] (array(nr_elements))

  [COLOR=#0000ff]! read array elements from the file[/color]
  [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], nr_elements
    [COLOR=#804040][b]read[/b][/color]([COLOR=#ff00ff]1[/color],[COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]err[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#804040][b]20[/b][/color]) array(i)
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#ff00ff]'(a)'[/color],[COLOR=#804040][b]advance[/b][/color][COLOR=#804040][b]=[/b][/color][COLOR=#ff00ff]'no'[/color]) [COLOR=#ff00ff]'.'[/color]
  [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#0000ff]! next line[/color]
  [COLOR=#804040][b]close[/b][/color]([COLOR=#ff00ff]1[/color])

  [COLOR=#0000ff]![/color]
  [COLOR=#6a5acd]20[/color] [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'End of Reading Data'[/color]
[COLOR=#a020f0]end subroutine[/color] read_data

[COLOR=#a020f0]subroutine[/color] print_array(array_name, array)
  [COLOR=#2e8b57][b]implicit[/b][/color] [COLOR=#2e8b57][b]none[/b][/color]
  [COLOR=#2e8b57][b]integer[/b][/color] :: n, i
  [COLOR=#2e8b57][b]double precision[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color], [COLOR=#2e8b57][b]dimension[/b][/color](:), [COLOR=#2e8b57][b]intent[/b][/color]([COLOR=#2e8b57][b]in[/b][/color]) :: array
  [COLOR=#2e8b57][b]character[/b][/color] :: array_name

  [COLOR=#0000ff]! process only if the array is allocated[/color]
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#008080]allocated[/color](array)) [COLOR=#804040][b]then[/b][/color]
    n [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]size[/color](array)
    [COLOR=#804040][b]do[/b][/color] i [COLOR=#804040][b]=[/b][/color] [COLOR=#ff00ff]1[/color], n
      [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) array_name, [COLOR=#ff00ff]'['[/color], i, [COLOR=#ff00ff]'] = '[/color], array(i)
    [COLOR=#804040][b]end do[/b][/color]
  [COLOR=#804040][b]else[/b][/color]
    [COLOR=#804040][b]write[/b][/color] ([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]) [COLOR=#ff00ff]'Error: Argument Array not allocated !'[/color]
  [COLOR=#804040][b]end if[/b][/color]
[COLOR=#a020f0]end subroutine[/color] print_array

[COLOR=#a020f0]end program[/color] read_subr
I have this sample data file:
inpt.dat
Code:
30 
50 
10 
80 
40 
20 
60
and so you can compile and run the above example:
Code:
$ g95 read_subr.f90 -o read_subr

$ read_subr
 Reading the Data from file 'inpt.dat' :
.......
 End of Reading Data

 Array of X-values: 
 X[ 1 ] =  30.
 X[ 2 ] =  50.
 X[ 3 ] =  10.
 X[ 4 ] =  80.
 X[ 5 ] =  40.
 X[ 6 ] =  20.
 X[ 7 ] =  60.

I hope this example helps you.
 
Thank you very very much for your answer!
Now i have found my mistake! The allocation and subroutines i had correct before too, but i forgot the word "contains" and wrote my subroutines under the program. I don't know actually what's the difference but now i works perfectly!

If I'll ever have a problam again with fortran i'll come back here again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top