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!

Passing allocatable arrays

Status
Not open for further replies.

chazhang

Technical User
Jul 9, 2009
4
CA
Hi,

I was wondering whats wrong with the way I'm passing my allocatable arrays. I've narrowed down the error to something similar to this:

program test_array
integer, allocatable :: test:))
integer n
allocate(test(5))
test = (/ 1,2,3,4,5 /)
call test_routine(test,n)
write(*,*),n
end

subroutine test_routine(test,n)
integer, allocatable :: test:))
integer n
n = size(test)
write(*,*), test
return
end

The thing is that it works in debug (for both 32 and 64 bit) and returns the value n = 5, but in release I always get n = 0 and test is blank.

Thanks in advance
 
You should use module or contains

This will work for you
test_array.f90
Code:
[COLOR=#a020f0]program[/color] test_array
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color] :: test(:)
  [COLOR=#2e8b57][b]integer[/b][/color] n
  [COLOR=#804040][b]allocate[/b][/color](test([COLOR=#ff00ff]5[/color]))
  test [COLOR=#804040][b]=[/b][/color] ([COLOR=#804040][b]/[/b][/color] [COLOR=#ff00ff]1[/color],[COLOR=#ff00ff]2[/color],[COLOR=#ff00ff]3[/color],[COLOR=#ff00ff]4[/color],[COLOR=#ff00ff]5[/color] [COLOR=#804040][b]/[/b][/color])
  [COLOR=#a020f0]call[/color] test_routine(test,n)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]),n

[COLOR=#a020f0]contains[/color]
  [COLOR=#a020f0]subroutine[/color] test_routine(test,n)
  [COLOR=#2e8b57][b]integer[/b][/color], [COLOR=#2e8b57][b]allocatable[/b][/color] :: test(:)
  [COLOR=#2e8b57][b]integer[/b][/color] n
  n [COLOR=#804040][b]=[/b][/color] [COLOR=#008080]size[/color](test)
  [COLOR=#804040][b]write[/b][/color]([COLOR=#804040][b]*[/b][/color],[COLOR=#804040][b]*[/b][/color]), test
  [COLOR=#804040][b]return[/b][/color]
  [COLOR=#a020f0]end subroutine[/color] test_routine
[COLOR=#a020f0]end program[/color] test_array
Output:
Code:
$ g95 test_array.f90 -o test_array

$ test_array
 1 2 3 4 5
 5
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top