Re: ALLOCATABLE arrays

From: Tony Jay (Tony.Jay_at_nospam.bsac.com)
Date: 01/05/04


Date: Mon, 5 Jan 2004 14:29:13 -0000


> Do I read this right?
>
> Suppose I declare
>
> subroutine test
> integer :: i
> dimension i(k)
>
> where k is set in a module somewhere. Is it then the case that
> a) i gets allocated automatically on entry to test and deallocated
> on exit
> b) this operation takes (essentially) zero time?
>
> Is that really all there is to it? I'm afraid to look a gift horse
> in the mouth, but this sounds too good to be true!
>
>
Gus,

This technique can cause problems if you have insufficient stack size
(especially if ou add in a second array at a later date)

My understanding is ...

If try to create a very large array on the stack and you do not have enough
stack (set up in the linking step of the compiler i think) then you program
will fail at run time.

Allocating on the heap gives you access to a hell of a lot more memory (well
as much as your OS can find !)

The typical allocate on the stack problem is to test with small problems and
then end users use large problems bomb out. You then look a fool (speaking
from personal experience)

The stack size can be increased of an excutable, but is not very pretty - it
also means setting up all your compilers the same way if you are compiling
cross platform.

As suggested by others, wrap the array into a module , add a new and delete
function in the module, perhaps add some clever logic with saved variables
to stop you (or someone else) allocating twice / deallocating twice and bobs
your auncle.

Tony

>



Relevant Pages

  • Re: Do buffers always start with the lowest memory address being the first element?
    ... > The C standard does not assume a downward-growing stack, ... > an upward-growing stack. ... C allows but does not require that the array produced ... > machine depends on both the C compiler and the machine. ...
    (comp.lang.c)
  • Re: Do buffers always start with the lowest memory address being the first element?
    ... The C standard does not assume a downward-growing stack, ... C allows but does not require that the array produced ... There is nothing stopping an actual C compiler on real hardware ... How are static locals in main ...
    (comp.lang.c)
  • Re: Really wierd bug
    ... > it has to be a compiler bug because it works fine for other population ... > My compiler is Compaq Visual Fortran 1997-1999. ... > with allocating the array sizes because I've defined the arrays as ... your program does indeed contain an array bounds error -- this ...
    (comp.lang.fortran)
  • Re: Interview question !!!
    ... Show me a compiler and platform with a stack where this ... variable, pretending it is a pointer to an array of such variables, ... A compiler that sees that fnever modifies x after it is ...
    (comp.lang.c)
  • Re: Large arrays: malloc or just declare?
    ... > some compiler/linker problems with the borland compiler. ... > stack vs heap, my google searches haven't inspired me. ... Method A will only work if you know the size of the array at compile time. ...
    (comp.lang.c)