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

Strange behavior in constructor of derived type with allocatable string

hs-napa
Beginner
187 Views
Hi,

Running the following simple program gives strange output.


[bash] MODULE TEST_MOD
   IMPLICIT NONE

   TYPE STRING
      CHARACTER(LEN=:), ALLOCATABLE :: S
   END TYPE

END MODULE


PROGRAM TEST
   USE TEST_MOD

   IMPLICIT NONE

   TYPE(STRING) :: A, B, C

   A = STRING('ABC')
   B = STRING('ABCDEFG')
   C = STRING('ABCDEFGHIJ')

   WRITE(*,*) A % S, LEN(A % S)
   WRITE(*,*) B % S, LEN(B % S)
   WRITE(*,*) C % S, LEN(C % S)

END PROGRAM

[/bash]
Compiling and running the program gives the following output:

>ifort constructor.f90
Intel Visual Fortran Compiler Professional for applications running on IA-32, Version 11.1 Build 20091130 Package ID: w_cprof_p_11.1.054
Copyright (C) 1985-2009 Intel Corporation. All rights reserved.

Microsoft Incremental Linker Version 9.00.30729.01
Copyright (C) Microsoft Corporation. All rights reserved.

-out:constructor.exe
-subsystem:console
constructor.obj

>constructor.exe
ABC 3
ABC 3
ABC 3

All the allocatable strings are the same length. When changing the order of constructors as follows:

[bash]   A = STRING('ABCDEFGHIJ')
   B = STRING('ABCDEFG')
   C = STRING('ABC')

[/bash]
and recompiling and running the test program gives the following output:

>constructor.exe
ABCDEFGHIJ 10
ABCDEFG 10
ABC 10

What the ...? I naively assumed to get strings with different lengths.

Again, if the initialization of A, B and C is changed to:

[bash]   A % S = 'ABC'
   B % S = 'ABCDEFG'
   C % S = 'ABCDEFGHIJ'

[/bash]
The output is what I expected to get in the first case:

>constructor.exe
ABC 3
ABCDEFG 7
ABCDEFGHIJ 10

Is this a feature or a bug?

BR,
-Heikki
0 Kudos
1 Reply
mecej4
Honored Contributor III
187 Views
This error does not occur with the more recent releases 11.1.065 or 11.1.067.
0 Kudos
Reply