Re: output of allocatable array of strings==> blank?



dpb wrote:
Mike wrote:
Hi

I had a subroutine which outputs array of strings, e.g. subheader, and
no_of_strings . Inside the subroutine, I declare subheader as
allocatable. I've checked the results in subroutine. It's ok.
However, they turn out blank in the main program. In main program, I
declare the subheader as a BIG_NUMBER of strings array.
(1) Why?
(2) could I declare as allocatable array of strings with no_of_strings
elements in main program? I worry about they will be blank. Or I
should write a function returning no_of_strings at first, then allocate
the subheader array, and then call the subroutine. That'll be
cumbersome.
...

You don't provide an example of the calling interface to see precisely,
but it sounds like the answer to (1) is probably because the
ALLOCATABLE array is created in the subroutine and therefore is local
to that routine.

For (2), the last option is "cleanest" from the standpoint of
determining the precise size of the array needed, but is, as you note,
less convenient operationally.

If you know (as I gather you don't :) ) the number a priori, you can
ALLOCATE the array of the right size and pass it, otherwise you can
create a large array as you presently are and pass it to the
subroutine. The operational problem is, you can't "shrink" that array
in the subroutine to the determined size so you would then either just
have to live w/ the full array and only use the populated portion in
the rest of the code logic or you could, on return, ALLOCATE a new
array based on the determined size and copy the elements to it and then
DEALLOCATE the working array. Probably not worth the effort as unless
the length is really large the amount of memory saved is likely to be
miniscule.

Allow me to restate my problem: selected sections of programs are as
follows:
program main
!C1 character*100 :: subheader(1000)
character*100, allocatable, intent(inout) :: subheader(:)
character*10000 par_name

!Q1 didn't allocate in advance, which I thought I should.
call parse_comma_delimited_header(par_name,subheader, no_data)
! here I cannot allocate(subheader) again, which I'd done
it. I know it now.
! Q2 print out the contents of subheader
stop
end
subroutine parse_comma_delimited_header(istr,subheader, no_data)
character*10000, intent(in) :: istr
integer, intent(out):: no_data
!C2 character*100 , intent(out):: subheader(1000)
character*100, allocatable, intent(inout) :: subheader(:)
allocate(subheader(no_data))
....... do stuffs & don't deallocate the subheader strings
return
end subroutine parse_comma_delimited_header

I've done a few tests. If I uncomment C statements, i.e. not using
allocatable, of course in main & sub it works. If I uncomment !C1 (not
using allocatable) and use !C2 (use allocatable in subroutine), I get
blank strings in main program. why? I am still confused.
However, if I comment !C statements, i.e. using allocatable. Then I
have the right subheader, i.e. not blank strings, in the main.
However, program stops because of "array bound exceeded".
(1) why? in !Q2 I print out no_data and it shows the right numbers.
That means in the subroutine it returns the right numbers. Furthremore
I've done " allocate(subheader(no_data))" in subroutine. why is
the array bound exceeded ?
(2)In !Q1, I didn't allocate in advance, which I thought I should.
Before, I seldom use allocate feature. Thank you and Rich mentioned
about "TR 15581". I've checked the internet and find infos about
allocate attribute. It mentioned about "Any actual argument that is
passed to an allocatable dummy array must itself be an allocatable
array". I find it in
http://www.zdv.uni-tuebingen.de/static/hard/zrsinfo/x86_64/nag/f95.5/lib/html/TR.html.
Oh why isn't it hyperlinked in this editor? Could you tell me a formal
site or keyword to search features of these in F95?
thank you very much.
Mike

.



Relevant Pages

  • output of allocatable array of strings==> blank?
    ... Inside the subroutine, I declare subheader as ... declare the subheader as a BIG_NUMBER of strings array. ... should write a function returning no_of_strings at first, then allocate ...
    (comp.lang.fortran)
  • Re: Efficient arrays for artificial neural networks?
    ... This method is obviously unwieldy as the array will be very sparse. ... elemental function num_rowsresult ... subroutine set_row ... allocate %ii) ...
    (comp.lang.fortran)
  • User defined type with allocatable arrays
    ... and I allocate them like this in the main program ... Here nx*ny is the length of the array that I want to allocate...(it's ... %derxinto a subroutine and modify them.. ... I am supposed to have an explicit interface irrespective of ...
    (comp.lang.fortran)
  • Re: output of allocatable array of strings==> blank?
    ... Inside the subroutine, I declare subheader as ... declare the subheader as a BIG_NUMBER of strings array. ... ALLOCATE the array of the right size and pass it, ...
    (comp.lang.fortran)
  • Re: output of allocatable array of strings==> blank?
    ... Inside the subroutine, I declare subheader as ... declare the subheader as a BIG_NUMBER of strings array. ... ALLOCATE the array of the right size and pass it, ...
    (comp.lang.fortran)