Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29235 Discussions

How to acheive a character length allocation using a non-parameter variable for the length type parameter

FlyingHermes
New Contributor I
292 Views
Hi,

I've notice that depending on the compiler used, the following program fails or succeeds compilation.
The program is related to type character length allocation using deferred type parameters.

The ifort compiler successds tha compilation whereas the gfortran fails.

Here is the program:

[fortran]Program test
  integer :: N1=5
  integer ,parameter:: N2=5
  character(len=:), allocatable :: string1, string2
  allocate(character(len=N1) :: string1) ! Problem with gfortran: "Error: Variable 'n1' cannot appear in the expression at (1)"
  allocate(character(len=N2) :: string2)
  write(6,*) len(string1)
  write(6,*) len(string2)
End Program[/fortran]
The problem encontered by gfortran is that the N1 integer variable is used in the "allocate" statement to set the length of the string but this variable (N1) is not defined with the parameter attribute.

How could I allocate a character with a non-parameter integer variable ?
I know that this can be acheived using parameterized derived types.
Can it be acheived by a simpler way ?

Thanks.
0 Kudos
1 Reply
IanH
Honored Contributor III
292 Views
You have hit a gfortran missing feature or bug - see http://gcc.gnu.org/bugzilla/show_bug.cgi?id=45170#c14. It erroneously rejects expressions that aren't specification expressions in length specifiers.

if your source must compile with both compilers then comments in the bug report give you a work-around alternative to the allocate statement such as:

string1 = repeat(' ',N1)
0 Kudos
Reply