Extended ALLOCATE statement

FTN95 allows you to access memory that was created in another process. The keyword SHARENAME is provided as an FTN95 extension to the standard ALLOCATE command in order to create memory in one process that can be accessed from another. The same keyword can also be used to create a file mapping.

FTN95 also allows you to access memory that was created elsewhere in the same process via the keyword ABSOLUTE_ADDRESS as descibed below.

Sharing memory between different processes

In the following illustration we create two programs that share common memory. The two programs (called shareA and shareB) are listed below. After compiling the programs using FTN95, the operation is as follows...

1. Start up program shareA.
2. Start up program shareB in a separate Command Prompt window.
3. Type a message as input into the instance of shareA.
4. View the same message as it appears as output in instance of shareB

The two programs use a common semaphore in order to synchronise the events.

Here is the code for shareA.f90...

include <windows.ins>
integer(2) c
  allocate(msg, SHARENAME="MyMemory")
  print*, "Type a message to send. Do not use spaces..."
  read*, msg
  call SIGNAL_SEMAPHORE@("MySemaphore")
  print*, "Read the message then press any key to terminate"
  call get_key@(c)

Here is the code for shareB.f90...

include <windows.ins>
integer(2) c
  allocate(msg, SHARENAME="MyMemory")
  print*, "Waiting for message..."
  call WAIT_ON_SEMAPHORE@("MySemaphore")
  print*, msg(1:len_trim(msg))
  print*, "Press any key to terminate"
  call get_key@(c)

File mapping

FTN95 also provides for the file-mapping of memory using the same keyword SHARENAME.

Here is some sample code to illustrate how it works. When you run the following program, the relevant file is created on the first run and there is no output. On the second run, the file now exists and its content is output.

character(*),parameter:: myData = "C:\TechSupport\test.dat"
logical file_exists@, exists
exists = file_exists@(myData)
allocate(msg, SHARENAME=myData)
if(.NOT. exists) then
  msg = "Data created!"
  print*, msg

If the drive letter for the path is lower case then the file is mapped for reading only. A file mapping occurs when the second character of the SHARENAME is a colon, otherwise you get inter-process shared memory without file mapping

De-allocating SHARENAME memory

By default the system releases shared memory when all processes accessing that memory have terminated. In exceptional circumstances you may wish to call DEALLOCATE for this memory allocation, in which case you must first call the subroutine ENABLE_DEALLOCATE@ (giving the name of the allocated variable) before calling DEALLOCATE.


For Win32 and x64 platforms, an FTN95 ALLOCATE statement can take the keyword argument ABSOLUTE_ADDRESS=addr where addr is the INTEGER KIND(7) address obtained from the LOC of some object. Here is an illustrative example.

INTEGER(7) addr 
addr = LOC(x) 
x = 3.14159 
PRINT*, this 



Copyright © 1999-2021 Silverfrost Limited