Re: can an allocatable array be really dynamic?



zhngbn@xxxxxxxxxxxxxxxx wrote:
If i have no idea of the exact amount of data that will be used, can i
still use the allocatable array? In other words, can the allocatable
array work like a linklist to allocate a node when needed? I donot want
to build a linklist because it is really time consuming when I try to
randomly use the data.Thank you!

The Fortran 2003 standard includes an intrinsic which allows for adding to an existing allocatable array without erasing the existing data. This isn't currently implemented in any compilers (which are still all Fortran 95 these days), but it's not that hard to do yourself in a slightly less efficient manner, with something like the following.

type expandable_array
real, pointer :: data(:)
integer :: ubound
integer :: data_ubound
end type

subroutine allocate_expandable(a, u)
type(expandable_array) :: a
integer :: u

a%ubound = u
a%data_ubound = u + 10
allocate(a%data(a%data_ubound))
end subroutine

subroutine reallocate_expandable(a, u)
type(expandable_array) :: a
real, pointer :: temp_data(:)
integer :: u

if u > a%data_ubound
a%data_ubound = a%data_ubound + 10
allocate(temp(1:a%data_ubound))
temp(1:a%ubound) = a(1:a%ubound)
deallocate(a%data)
a%data => temp
end if

a%ubound = u
end subroutine

That's unfinished in a number of ways; most notably, the choice of a new value for a%data_ubound is exceptionally poor -- it should be guaranteed to be larger than u, for one thing, and 10 may not be the most efficient number to use -- you want to pick something that balances the extra wasted memory against the number of times you have to reallocate. Also, since a%data is a pointer rather than an allocatable, you probably also want a deallocate_expandable subroutine.

- Brooks


--
The "bmoses-nospam" address is valid; no unmunging needed.
.



Relevant Pages

  • Re: array of polymorphic objects
    ... subroutine create ... class, allocatable:: obj! ... allocate(ty_ext1:: obj) ... procedure, pointer:: create ...
    (comp.lang.fortran)
  • upper and lower bounds of a dummy array
    ... without the POINTER or ALLOCATABLE attribute. ... does not want to allocate something or to change the association of the ... SUBROUTINE s1 ... So I would like to learn why upper bounds and lower bounds are not passed in ...
    (comp.lang.fortran)
  • Re: defined operator & assignment speed & memory usage problem
    ... module procedure assign_field_scalar ... subroutine assign_field ... allocate(left%data(fieldsize)) ...
    (comp.lang.fortran)
  • Re: allocatable and pointer in a type
    ... allocatable or pointer parameters use memory. ... they are not allocate! ... Thus, in order to find out exactly how it reports the use memory, you will need to read your vendor documentation. ... Third, allocatable arrays as components of derived types, a Fortran 2003 feature, occupy memory regardless of whether the allocatable array is actually allocated. ...
    (comp.lang.fortran)
  • Re: defined operator & assignment speed & memory usage problem
    ... module procedure assign_field_scalar ... subroutine assign_field ... allocate(left%data(fieldsize)) ...
    (comp.lang.fortran)