Re: allocatable array *not* equivalent to dynamic allocation?



Brooks Moses wrote:

As James said in his reply, this is talking about the fact that, with Fortran allocatable arrays, you cannot take an already-allocated array and reallocate it to a new size without losing the data in it.

Thus, if you (for example) have an allocatable array FOO with 16 values in it, and you want to add space for 16 more, you have to first copy those 16 values into a temporary array, deallocate FOO and then allocate it with size 32, and copy the saved values back into it from the temporary array.

This means that, when you allocate the array, you have to allocate it as the maximum size that you're going to use until you deallocate it, rather than allocating some of it now and more of it later.

(I also disagree with what appears to be the authors' claim that this is somehow different from "dynamic allocation". It may be different from how dynamic allocation is implemented in certain languages, but that's not the same thing at all.)

As I understand it (not having looked at it lately), even the "normal" dynamic memory access of the C++ (1998) standard library uses this form of extending something. It's just that it gets buried under the hood of the standard templates, something that isn't so easy to do in F95.

(I've been known to use the std::vector reallocation model in my own code, with an additional final "trimming" step to reduce the "allocated" length to the "in-use" length.)
.



Relevant Pages

  • Re: Cons cell archaic!?
    ... from s-expression or XML or other syntax you keep the bloated array ... For using vectors to emulate lists that ... Allocate 2, move 1 element: ... What do you think of that algorithm? ...
    (comp.lang.lisp)
  • Storing/Retrieving TYPEs with ALLOCATABLE components (TR) (long)
    ... tBrd, including array descriptor of tEn )). ... Without previous DEALLOCATE, the allocate line fails at run time with message ... the fact that I'm loading an invalid descriptor tBrd%tEn from the file... ... status (which is not possible according to Standard, but then BINARY files ...
    (comp.lang.fortran)
  • Re: Storing the size of an array in the structure itself
    ... >> I think every C programmer can relate to the frustrations that malloc ... >> the size of an array must be stored separately to be a nightmare. ... is anything more than just that - a chunk of memory. ... > Otherwise you couldn't tell it how much to allocate. ...
    (comp.lang.c)
  • determining available space for Float32, for instance
    ... I am looking for a way to determine the maxium array size I can allocate ... We do not want a solution that requires recompiling Python, ... agents may be households that choose a new gridcell to live in. ... Each attribute of a dataset has such a 2D array. ...
    (comp.lang.python)
  • 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)