GCC Newspaper
JUNE 15, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran

ALLOCATE of parameterized derived-type array initializes only first element

A Fortran compiler bug incorrectly initialized only the first element of parameterized derived-type arrays during allocation, leading to crashes due to uniniti…

When allocating arrays of parameterized derived types in Fortran, only the first element’s allocatable components were being initialized. This was due to an incorrect rank being passed to the initialization routines; expr->rank was 0 for array allocation expressions, even though the symbol itself represented an array. Consequently, access to subsequent elements would result in use of uninitialized pointers and program crashes. The compiler now correctly determines the array rank from the GFC array descriptor, ensuring all elements are properly initialized.

In Details

The Fortran frontend's trans-stmt.cc file handles the translation of allocate and deallocate statements. Specifically, gfc_trans_allocate and gfc_trans_deallocate manage allocation and deallocation for parameterized derived types (PDT). The bug stemmed from using expr->rank directly, which for an allocate-shape-spec expression (e.g., ALLOCATE(a(N))) is 0. The correct rank for array processing resides in the symbol's descriptor via GFC_TYPE_ARRAY_RANK. This oversight caused a loop intended to initialize allocatable components for each array element to run zero times, leaving elemen…

For Context

Compilers translate human-readable source code into machine code. Fortran allows users to define custom data structures called 'derived types', which can have 'parameters' (like a template) and 'allocatable components' (parts that can be dynamically sized). When Fortran code allocates an array of these special derived types, the compiler needs to initialize each element of the array correctly. A bug caused the compiler to only initialize the very first element of such arrays, leaving the rest of the array elements with garbage values. When the program later tried to use these uninitialized elements, it would often crash. The fix ensures the compiler correctly identifies the size of the array and initializes all its elements properly during the allocation process.

Filed Under: fortrancompiler-bugmemory-initialization