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

Allocatable character substrings

John4
Valued Contributor I
1,111 Views

Hi, I've noticed that allocatable character substrings passed to a procedure as arguments are not handled properly (see code below); at least in my computer, instead of upper-case, I get random garbage for the "text" variable. Is it a bug in the compiler or a "requirement" of the Fortran standard (i.e., is the code illegal?)?

program test_allocatable_character
implicit none
integer :: i
character(:), allocatable :: text
character(16) :: text2
integer, parameter :: IBLANK = IACHAR(' ')
text = 'nevermore'
text2 = text
write (*, '(8X, A, A)') 'allocatable ', 'buffer'
do i = 1, MIN(LEN_TRIM(text), LEN_TRIM(text2))
write (*, '("char(", I0, "): ", 5X, A, 9X, A)') i, upper_case(text(i:i)), upper_case(text2(i:i))
enddo
contains
elemental function upper_case(c) result(UC)
character :: UC
character, intent(IN) :: c
integer :: i
continue
i = IACHAR(c)
select case (i)
case (97:122); UC = ACHAR(i - IBLANK)
case default
UC = c
end select
end function
end program test_allocatable_character

0 Kudos
1 Solution
Steven_L_Intel1
Employee
1,111 Views
This looks like another bug, sad to say. I'll report it to the developers.

View solution in original post

0 Kudos
7 Replies
TimP
Honored Contributor III
1,111 Views
Quoting - John

Hi, I've noticed that allocatable character substrings passed to a procedure as arguments are not handled properly (see code below); at least in my computer, instead of upper-case, I get random garbage for the "text" variable. Is it a bug in the compiler or a "requirement" of the Fortran standard (i.e., is the code illegal?)?

program test_allocatable_character
implicit none
integer :: i
character(:), allocatable :: text
character(16) :: text2
integer, parameter :: IBLANK = IACHAR(' ')
text = 'nevermore'
text2 = text
write (*, '(8X, A, A)') 'allocatable ', 'buffer'
do i = 1, MIN(LEN_TRIM(text), LEN_TRIM(text2))
write (*, '("char(", I0, "): ", 5X, A, 9X, A)') i, upper_case(text(i:i)), upper_case(text2(i:i))
enddo
contains
elemental function upper_case(c) result(UC)
character :: UC
character, intent(IN) :: c
integer :: i
continue
i = IACHAR(c)
select case (i)
case (97:122); UC = ACHAR(i - IBLANK)
case default
UC = c
end select
end function
end program test_allocatable_character

john.f90(11): error #12040: actual argument 1 in call of "TEST_ALLOCATABLE_CHARA
CTER_ip_UPPER_CASE" doesn't conform to formal argument. See (file:john.f90 line:
14)
john.f90(11): error #12040: actual argument 3 in call of "TEST_ALLOCATABLE_CHARA
CTER_ip_UPPER_CASE" doesn't conform to formal argument. See (file:john.f90 line:
14)
john.f90(11): error #12145: function "TEST_ALLOCATABLE_CHARACTER_ip_UPPER_CASE"
is called as subroutine

$ gfortran -O john.f90
john.f90:4.10:

character(:), allocatable :: text
1
Error: Syntax error in CHARACTER declaration at (1)
john.f90:11.66:

write (*, '("char(", I0, "): ", 5X, A, 9X, A)') i, upper_case(text(i:i)), upper
1
Error: Syntax error in argument list at (1)
john.f90:7.4:

text = 'nevermore'
1
Error: Symbol 'text' at (1) has no IMPLICIT type

The example seems to combine several difficult syntax features, which not all compilers implement, let along diagnose problems correctly.
Even in MR&C, the example of allocatable scalar doesn't rely on automatic allocation.
I can't find a discussion of it in Adams, Brainerd, and the link to Fortran standard on the forum header page isn't working today.
0 Kudos
John4
Valued Contributor I
1,111 Views
Quoting - tim18
The example seems to combine several difficult syntax features, which not all compilers implement, let along diagnose problems correctly.
Even in MR&C, the example of allocatable scalar doesn't rely on automatic allocation.
I can't find a discussion of it in Adams, Brainerd, and the link to Fortran standard on the forum header page isn't working today.

Hi tim18,

In Intel Fortran 11.1(.046?), the allocatable character feature is already implemented, although it's still buggy ---that's why I asked if the improper handling of substrings as arguments is a bug in the current implementation or if allocatable character substrings are not allowed to be passed, according the Fortran 2003 standard.

Allocatable characters are not yet implemented in gfortran, according to this page: http://gcc.gnu.org/wiki/Fortran2003Status.

0 Kudos
Steven_L_Intel1
Employee
1,112 Views
This looks like another bug, sad to say. I'll report it to the developers.
0 Kudos
Steven_L_Intel1
Employee
1,111 Views
The problem occurs any time you pass a substring of a deferred-length character variable - what gets passed is the address of the pointer rather than the address of the data. If you pass the whole variable, it's fine. Issue ID is DPD200140685.
0 Kudos
Steven_L_Intel1
Employee
1,111 Views
This bug has been fixed in our sources. The fix should appear in Update 4, currently scheduled for the second half of December.
0 Kudos
Steven_L_Intel1
Employee
1,111 Views
This issue is resolved in Update 4, available now.
0 Kudos
intelplayer
Beginner
1,111 Views
This issue is resolved in Update 4, available now.
good to know that it's done resolved greats
0 Kudos
Reply