PRB: Using FORTRAN Input/Output Statements with Basic (62215)



The information in this article applies to:

  • Microsoft QuickBASIC 4.5
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0

This article was previously published under Q62215

SYMPTOMS

If a compiled Basic program is linked with a large or huge memory model FORTRAN routine that uses either CHARACTER data or the FORTRAN READ or WRITE statements, the Basic program will not be able to access the far heap.

RESOLUTION

To avoid this problem, link only to FORTRAN routines compiled in the medium memory model.

FORTRAN routines placed in a Quick library will work correctly, even if the FORTRAN is compiled in the large or huge memory model.

MORE INFORMATION

A number of symptoms can indicate the problem above. Here are two:

  1. Basic function FRE(-1), which should return the total available memory (far heap + data segment), returns the same amount of space as the available data segment space. The amount of available data segment space can also be obtained by FRE(0), and if Basic cannot access the far heap due to the above problem, the amount returned by these two functions will be equal.
  2. Basic will report "Out of memory" when trying to dimension a $DYNAMIC array larger than the remaining space in the data segment. Since Basic cannot access the far heap, the free portion of the data segment is the only available memory, and memory will be exhausted if the array is larger than the space remaining in the data segment.
Below are two FORTRAN examples that demonstrate the problem. Program 1 shows a FORTRAN call without I/O statements. Program 2 shows the same program with one WRITE statement included.

NOTE: The Basic program does not even have to call the FORTRAN subprogram; it is the allocation of the FORTRAN data space that causes the error.

To compile the FORTRAN.OBJ, use the following commands:
   FL /C /FPi /AL FORPROG1.FOR;    'for subprogram #1
   FL /C /FPi /AL FORPROG2.FOR;    'for subprogram #2
				
To compile the Basic.OBJ, use the following command:
   BC /A /O /AH BASPROG.BAS;
				
Then, link the files together as follows:
   LINK /NOE BASPROG+FORPROG1,PROG1;   'for program #1
   LINK /NOE BASPROG+FORPROG2,PROG2;   'for program #2
				
To run the programs, type either PROG1 or PROG2.

Code Samples

   '-------BASPROG---------------------------
   DECLARE SUB ForSub ()
   DIM heap%(2048)
   COMMON /nmalloc/ heap%()
   PRINT "In Basic...memory = "; FRE(-1)
   CALL ForSub
   PRINT "Back in Basic.  memory = "; FRE(-1)
   END
				

FORTRAN Program 1

         subroutine forsub
         RETURN
         END
				

FORTRAN Program 2

         subroutine forsub
         WRITE(*,*)'This is the FORTRAN program.'
         RETURN
         END
				

OUTPUT

Program 1

In Basic...memory = 416968
Back in Basic. memory = 416968

Program 2

In Basic...memory = 53992
Back in Basic memory = 53992

WORKAROUND

If the above example is used, but the FORTRAN compiler is instructed to use the medium memory model (/AM instead of /AL) and the library MLIBFORE is linked instead of LLIBFORE, the program will work correctly.

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:kbprb KB62215