Re: allocation error




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.

Put the allocate after the read.
You'd think I would have done it like malloc.
program pascal1
IMPLICIT NONE
integer, dimension(:), allocatable :: pascal
integer :: limit, i, j
write (*,'(a)', advance='no') 'give me an int less than 14: '
read (*, '(i2)') limit
allocate(pascal(0:limit))
!initialize
do i=0, limit
pascal(i) = 0
end do
pascal(limit) = 1
!outer loop
do j=1,limit
do i=1, limit
pascal(i-1)=pascal(i-1)+pascal(i)
if (pascal(i).eq.0) then
write (*,'(a)',advance='no') " "
else
write (*,'(i4)',advance='no') pascal(i)
end if
end do
write(*,*)
end do
!done
deallocate (pascal)
end program pascal1
This turns out to be a nifty little program once you get some traction
on it (MR&C 4.7.3). I'm simply surprised that the output looks as nice
as it does. (I still remember the headache it was in college trying to
get fortran output to look proper, but that was a different century.)

One thing I really miss from C while I'm coding is the C-style
comments, /* ... */, that envelope a problem block while you're shaking
the bugs out. Does fortran have something other than '!'? LS

.



Relevant Pages

  • Re: allocation error
    ... end program pascal1 ... This program wants to dynamically allocate an array of rank 1. ... It isn't a bug. ... *** Error 112, Reference to undefined variable, array element or ...
    (comp.lang.fortran)
  • allocation error
    ... end program pascal1 ... This program wants to dynamically allocate an array of rank 1. ... Run-time Error ... *** Error 112, Reference to undefined variable, array element or ...
    (comp.lang.fortran)
  • Re: allocation error
    ... write ', advance='no') 'give me an int' ... That's because your allocate is before you read the value of n. ... is an executable statement. ... Or is the undefined status of n a non-required error ...
    (comp.lang.fortran)
  • Re: allocation error
    ... That's because your allocate is before you read the value of n. ... is an executable statement. ... I have made the OP's error of allocating an array to a size specified ... is randomly set to some very large value, ...
    (comp.lang.fortran)
  • Re: allocation error
    ... lane straatman wrote: ... write ', advance='no') 'give me an int' ... That's because your allocate is before you read the value of n. ... is an executable statement. ...
    (comp.lang.fortran)