Re: allocation error



dpb wrote:
Beliavsky wrote:
dpb wrote:
Richard Maine wrote:
lane straatman <grumpy196884@xxxxxxxxxxx> wrote:

allocate(pascal(1:n))
write (*,'(a)', advance='no') 'give me an int'
read (*, '(i3)') n
...
*** Error 112, Reference to undefined variable, array element or
function result (/UNDEF)
That's because your allocate is before you read the value of n. Allocate
is an executable statement. It does its thing when it is executed, which
in this case is before n is defined. My guess is that you are thinking
it does something more complicated like make the array size track the
value of n as n changes. Nope, it is much simpler than that.
Question, Richard--would the use of a STAT variable if it had been used
(and subsequently tested, of course) been required to catch this
particular error? Or is the undefined status of n a non-required error
to be caught?
The following does not directly answer your question.

I have made the OP's error of allocating an array to a size specified
by a variable that has not been set. What typically happens is that "n"
is randomly set to some very large value, causing the ALLOCATE to fail
and the program to crash (I don't use a STAT with the ALLOCATE). For me
this works well, since I quickly notice the bug and fix it.

I retract the question on looking more closely at the original post --
I first mistakenly thought the error didn't refer to the ALLOCATE()
line, but the later reference. When I re-read the actual posted error
including the line number, I see the error/stop was associated with the
failure of ALLOCATE so the STAT variable would have returned non-zero.

Not necessarily. As soon as an undefined variable (in this case, n) is
referenced, all bets are off. Then, the consequence of the ALLOCATE can
potentially be anything -- no error, crash, reboot machine -- regardless of the
presence or absence of STAT. In this respect, Beliavsky's response is a little
misleading.

I guess a related question that I could try but don't have the compiler
handy just now is whether if the particular compiler does initialize n
to zero, does the zero-extent array constitute and error or is that
(I'm sorta' presuming) ok. The case you mention of a large, but
positive random value failing for lack of memory I see.


Again, if n is not properly initialized, then it's not really meaningful to ask
what the result will be.

But it *is* perfectly legal to allocate an array with size 0 -- so if one
initialized n to zero, then the allocate statement would be quite valid.

cheers,

Rich
.



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)