next up previous 216
Next: Include Files
Up: Examples
Previous: Create a file with name that is specific to a user


Get some virtual memory

One of the annoying features of FORTRAN 77 is that all storage space must be allocated at compile time, i.e. there are no dynamic arrays. Here is an example of using PSX routines to dynamically allocate an array.

      PROGRAM MAIN
      IMPLICIT NONE
      INCLUDE 'SAE_PAR'
      INTEGER STATUS

* Set the STATUS to OK.
      STATUS = SAI__OK

* Call the subroutine.
      CALL GETVM( STATUS )

      END


      SUBROUTINE GETVM( STATUS )
      IMPLICIT NONE
      INCLUDE 'SAE_PAR'
      INCLUDE 'CNF_PAR'
      INTEGER STATUS, PNTR

* Check global status.
      IF( STATUS .NE. SAI__OK ) RETURN

* Create a ten element integer array and return a pointer to it.
      CALL PSX_CALLOC( 10, '_INTEGER', PNTR, STATUS )

* If all is well, operate on the array.
      IF (STATUS .EQ. SAI__OK) THEN
         CALL FILL( %VAL(PNTR), 10 )
         CALL PRNT( %VAL(PNTR), 10 )
         CALL PSX_FREE( PNTR, STATUS )
      END IF

      END


      SUBROUTINE FILL( ARRAY, N )
* Put some numbers in the array.
      INTEGER N, ARRAY( N )
      INTEGER I

      DO I = 1, N
         ARRAY( I ) = I
      END DO

      END


      SUBROUTINE PRNT( ARRAY, N )
* Print the elements of ARRAY.
      INTEGER N, ARRAY( N )
      INTEGER I

      DO I = 1, N
         PRINT *,ARRAY( I )
      END DO

      END

In this case the main program merely sets the value of STATUS and calls the subroutine GETVM to do the work. Although this is more typing, it does have the advantage that this could be made into an ADAM task simply by deleting the main program. GETVM tests that the value of STATUS returned from PSX_CALLOC is OK, but does not print any error message. This is not necessary, as the PSX routines all report their own errors via the EMS routines. The reporting of error messages may be deferred if required, as described in SUN/104 and SSN/4.

If the code is required to work where pointers may be longer than INTEGERs, The construct %VAL(CNF_PVAL(PNTR)), rather than simply %VAL(PNTR), should be used in passing the pointer to FILL and PRNT. Function CNF_PVAL is defined in the CNF_PAR include file and described in SUN/209.



next up previous 216
Next: Include Files
Up: Examples
Previous: Create a file with name that is specific to a user

PSX POSIX interface routines
Starlink User Note 121
P M Allan
A J Chipperfield
10 April 2003
E-mail:ussc@star.rl.ac.uk

Copyright © 2000 Council for the Central Laboratory of the Research Councils