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

ID 767251
Date 6/24/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

Dynamic Allocation

Data objects can be static or dynamic. If a data object is static, a fixed amount of memory storage is created for it at compile time and is not freed until the program exits. If a data object is dynamic, memory storage for the object can be created (allocated), altered, or freed (deallocated) as a program executes.

Pointers, classes, deferred length character, allocatable scalars and arrays, and automatic arrays are dynamic data objects.

No storage space is created for a pointer until it is allocated with an ALLOCATE statement or until it is assigned to an allocated target. The storage space allocated is uninitialized.

An ALLOCATE statement can also be used to create storage for an allocatable array. A DEALLOCATE statement can be used to free the storage space reserved in a previous ALLOCATE statement. It also causes any pointers to become disassociated.

An unsaved allocatable that is declared in the specification part of a procedure or BLOCK construct and has a status of allocated when the procedure or BLOCK terminates is automatically deallocated at that time.

A pointer can be dynamically disassociated from a target by using a NULLIFY statement.

Automatic arrays differ from allocatable arrays in that they are automatically allocated and deallocated whenever you enter or leave a procedure, respectively.

Dynamic allocation occurs at runtime and is handled by the Fortran runtime library. Several restrictions on allocation and deallocation must be observed when these operations on a specific object are performed in program units that are separately compiled. When allocation and deallocation of an object are split between procedures in static code and dynamic shared libraries (.so files on Linux*) or dynamic-link libraries (DLLs on Windows*), the following applies:

  • If the dynamic library is compiled with the [q or Q]openmp compiler option, then the main program must be compiled and linked with [q or Q]openmp to include the OpenMP memory handling routines in the program.

  • If the dynamic library allocates data in High bandwidth (HBW) memory on Linux*, then the program must be linked with the libmemkind library to include the HBW memory handling routines in the program.

NOTE:

Dynamic memory allocation is limited by several factors, including swap file size and memory requirements of other applications that are running. Dynamic allocations that are too large or otherwise attempt to use the protected memory of other applications result in General Protection Fault errors.

If you encounter an unexpectedly low limit, you may need to reset your virtual memory size through the Control Panel or redefine the swap file size.

Some programming techniques can help minimize memory requirements, such as using one large array instead of two or more individual arrays. Allocated arrays that are no longer needed should be deallocated.

See Also