Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public
Document Table of Contents

Allocate Common Blocks

Use option [Q]dyncom to dynamically allocate common blocks at runtime.

This option designates a common block to be dynamic. The space for its data is allocated at runtime rather than at compile time. On entry to each routine containing a declaration of the dynamic common block, a check is performed to see whether space for the common block has been allocated. If the dynamic common block is not yet allocated, space is allocated at that time.

The following command-line example specifies the dynamic common option with the names of the common blocks to be allocated dynamically at runtime:

ifx -dyncom "blk1,blk2,blk3" test.f  ! Linux
ifx /Qdyncom "blk1,blk2,blk3" test.f ! Windows

where blk1, blk2, and blk3 are the names of the common blocks to be made dynamic.

Guidelines for the [Q]dyncom Option

The following are some limitations that you should be aware of when using option [Q]dyncom:

  • An entity in a dynamic common cannot be initialized in a DATA statement.

  • Only named common blocks can be designated as dynamic COMMON.

  • An entity in a dynamic common block must not be used in an EQUIVALENCE expression with an entity in a static common block or a DATA-initialized variable.

Advantage of a Dynamic Common Block

Dynamic common blocks let you supply your own allocation routine to control common block allocation. To use your own allocation routine, you should link it ahead of the Fortran runtime library.

The C function prototype is:

void _FTN_ALLOC(void **mem, int *size, char *name);

where

  • mem is the location of the base pointer of the common block that must be set by the routine to point to the block of memory allocated.

  • size is the integer number of bytes of memory that the compiler has determined are necessary to allocate for the common block as it was declared in the program.

    You can ignore this value and use whatever value is necessary for your purpose. You must return the size in bytes of the space you allocate. The library routine that calls the default or Fortran runtime function ensures that all other occurrences of this common block fit in the space you allocated. You can return the size in bytes of the space you allocate by modifying size.

  • name is the name of the common block being dynamically allocated.

The equivalent Fortran INTERFACE specification is the following:

interface
subroutine my_ftn_alloc (mem, size, name) bind (C, name="_FTN_ALLOC")
    use, intrinsic                :: ISO_C_BINDING
    implicit none

    type(C_PTR),    intent(OUT)         :: mem
    integer(C_INT), intent(INOUT)       :: size
    character, dimension(*), intent(IN) :: name
end subroutine my_ftn_alloc
end interface

You can also use the Fortran runtime function FOR__SET_FTN_ALLOC to specify your own allocation routine. This method is especially helpful when you are using shared libraries. If you choose this method, it overrides the default routine _FTN_ALLOC.

Allocate Memory to Dynamic Common Blocks

The runtime library routine f90_dyncom performs memory allocation. The compiler calls this routine at the beginning of each routine in a program that contains a dynamic common block. In turn, this library routine calls _FTN_ALLOC to allocate memory.

By default, the compiler passes the size in bytes of the common block as declared in each routine to f90_dyncom, and then on to _FTN_ALLOC. The Fortran runtime library contains a default version of _FTN_ALLOC, which simply allocates the requested number of bytes and returns.

If you use the nonstandard extension having the common block of the same name declared with different sizes in different routines, you may get a runtime error depending on the order in which the routines containing the common block declarations are invoked.

Creating your own routine to dynamically allocate common blocks is especially useful when you are sharing libraries. To use your own routine, you must specify the runtime function FOR__SET_FTN_ALLOC.

See Also