is there a way i can make my array allocatable after the declaration ? i'm thinking of making it allocatable (after declaration) Inside the subroutine allocating memory so that in don't have to change the declaration in my old program.
No.
The error message is very explicit. You can't pass a non allocatable array as argument to a function that is expecting an allocatable array.
The following declaration:
integer alist(1)
Is creating an array of 1 dimension of size 1.
If you want your arrays to be allocated dynamic, you must change them to allocatable or use pointers.
EDIT
As from here: http://web.stanford.edu/class/me200c/tutorial_77/12_arrays2.html
Most programmers prefer to use the asterisk notation to emphasize that the "real array length" is unknown. Some old Fortran 77 programs may declare variable length arrays like this:
real x(1), y(1)
This is legal syntax even if the array lengths are greater than one!
But this is poor programming style and is strongly discouraged.
P.S. This notation is used INSIDE subroutines to define variable length array arguments.
EDIT 2
The following code, based on yours, demonstrate the use of allocatable.
MODULE ARRAY_ALLOCATION
CONTAINS
SUBROUTINE ARRAY_ALLOCATE (ARR, ARR_SIZE, ARR_IDX, CODE_RET)
! DECLARE AN ALLOCATABLE PARAMETER
INTEGER, ALLOCATABLE, INTENT (INOUT) :: ARR(:)
INTEGER, INTENT (IN) :: ARR_SIZE,ARR_IDX,CODE_RET
INTEGER :: IDX_END
IDX_END = ARR_IDX + ARR_SIZE -1
ALLOCATE (ARR(ARR_IDX:IDX_END))
ARR = 1
RETURN
END SUBROUTINE ARRAY_ALLOCATE
SUBROUTINE ARRAY_DEALLOCATE (ARR)
INTEGER, ALLOCATABLE, INTENT (INOUT) :: ARR(:)
DEALLOCATE (ARR)
RETURN
END SUBROUTINE ARRAY_DEALLOCATE
subroutine Create (arr)
INTEGER, ALLOCATABLE, INTENT (INOUT) :: ARR(:)
call ARRAY_ALLOCATE(arr,5,3,1)
end subroutine Create
subroutine Destroy (arr)
INTEGER, ALLOCATABLE, INTENT (INOUT) :: ARR(:)
CALL ARRAY_DEALLOCATE(arr)
end subroutine Destroy
END MODULE
program Console1
USE ARRAY_ALLOCATION
implicit none
integer, allocatable :: alist(:)
if (allocated(alist)) then
print *, "is allocated"
else
print *, "is not allocated"
endif
call Create(alist)
if (allocated(alist)) then
print *, "is allocated"
else
print *, "is not allocated"
endif
print *, alist
CALL Destroy(alist)
if (allocated(alist)) then
print *, "is allocated"
else
print *, "is not allocated"
endif
end program Console1
The expected result would be something like this:
is not allocated
is allocated
1 1 1 1 1
is not allocated
EDIT 3
About allocatable arrays, a simple explanation on why someone would want to deallocate can be found here https://www.phy.ornl.gov/csep/pl/node17.html:
Allocatable arrays are those explicitly declared ALLOCATABLE. An allocatable array may be local to a procedure or may be placed in a module and effectively be global to all procedures of the application. An allocatable array is explicitly allocated with the ALLOCATE statement, and deallocated either explicitly with the DEALLOCATE statement or, if it is a local array for which SAVE has not been specified, automatically upon exit from the procedure. (If SAVE has been specified, local allocatable arrays can persist from one execution of the procedure to the next - they must be explicitly deallocated with a DEALLOCATE statement.) A global allocatable array persists until it is explicitly deallocated, which may occur in a procedure different from the one in which it was allocated.
As can be seen, there are situations where deallocating an allocatable array must be done manually, if necessary.