Re: Allocatable Arrays As Outputs



On Jan 30, 3:47 pm, Infinity77 <andrea.gav...@xxxxxxxxx> wrote:
Hi All,

first of all, please forgive my poor mastering of Fortran. I am
still a beginner who uses Fortran sporadically.

I have a subroutine in which I declare 4 output variables as
allocatable. This is because, a priori, I don't know their lengths
(they're all one-dimensional vectors), and I discover this information
only when I read a file which contains both their lengths and the data
used to fill these vectors.

My problem is, I don't know how to correctly "declare" these variables
in the main program, as I keep getting errors on assumed-shape/size
and dummy arguments. My knowledge of Fortran is so slim I can't find a
way to solve this problem.

I am attaching a simplified version of my small program (1 subroutine
+ the main) in which I have only 1 allocatable array. I have used my
google-fu but either I can't interpret correctly the problem or what I
am doing is completely wrong.

Thank you for all your suggestions.

Andrea.

subroutine ReadINSPEC(fileName, propertyNames)

use ReadECLIPSEBinary

implicit none

character (len=*), intent(in) :: filename
character (len=8), allocatable, intent(out) :: propertyNames(:)

integer numberOfProps
character (len=4) keywordType
character (len=8) keywordName

logical feof

open(unit=1, file=filename, form='UNFORMATTED',
convert='BIG_ENDIAN')

feof = .false.

while_loop: do while (.not.feof)

read(1, end=20, err=18) keywordName, numberOfProps,
keywordType

if (keywordName == 'NAME') then

allocate(propertyNames(numberOfProps))
call ReadECLIPSEData(1, numberOfProps,
stringVector=propertyNames)
exit while_loop

endif

18 continue

enddo while_loop

20 continue

deallocate(propertyNames)

close(1)
return

end subroutine ReadINSPEC

program main

! ???????
! I don't know if this is possible or not...
! ???????

character(len=14) :: fileName
character(len=8) :: propertyNames(:)

fileName = 'OPT_INJ.INSPEC'

call ReadINSPEC(fileName, propertyNames)

end program main

Hi,
You can try using pointers instead (they are pretty simple to use in
f95). If you declare propertynames as a pointer, I.e. replace
'allocatable' with 'pointer', you can still allocate memory as you
have. Then you simply need to declare the variable in the calling
program to also be a pointer (same shape). You also have to use the
save attribute in the subroutine to ensure the memory isnt lost when
the subroutine exits.

If you want to call this routine more than once using this approach
you'll want to assign new memory in the main program (i.e allocate a
new variable), and add something to the start of the subroutine to
free up the memory.

Others may know a better way

Hope this is useful
.



Relevant Pages

  • Allocatable Arrays As Outputs
    ... please forgive my poor mastering of Fortran. ... I don't know how to correctly "declare" these variables ... I am attaching a simplified version of my small program (1 subroutine ... readkeywordName, numberOfProps, ...
    (comp.lang.fortran)
  • Re: Creating AutoText
    ... ..Saved property to True. ... - If you declare a variable between a Sub statement and its matching ... that variable exists only while that subroutine is executing ... >>> Dim strATCategory As String ...
    (microsoft.public.word.vba.general)
  • Re: Can anyone see why this skips the remaining username/pass hash elements
    ... this subroutine within a loop block. ... only to the variables declared in the FIRST iteration of that loop. ... Alternatively, declare the %array variable ...
    (comp.lang.perl.misc)
  • using my problem
    ... In order to use the array @files in the below code outside of the ... subroutine, so I need to declare @files globally? ... where is the correct place to declare them in terms of neatness? ... addressee, any disclosure, reproduction, distribution or other ...
    (perl.beginners)
  • Re: Can anyone see why this skips the remaining username/pass hash elements
    ... this subroutine within a loop block. ... only to the variables declared in the FIRST iteration of that loop. ... Alternatively, declare the %array variable ... my $num = shift; ...
    (comp.lang.perl.misc)