The following code does some 'voodoo' using a C pointer, TRANSFER intrinsic, and what not, but it is standard-conforming as far as I can tell. It functions as expected when compiled using gfortran, however it runs into a program exception with Intel Fortran compiler. Note the exception arises with objects of ALLOCATABLE attribute; if line 7 is commented out and line 8 is uncommented in the main program, it works ok with Intel Fortran compiler and so it does with gfortran.
module m use, intrinsic :: iso_c_binding, only : c_loc, c_ptr, c_f_pointer implicit none private public :: get_s character(len=*), parameter :: s = "Hello World!" contains subroutine get_s( t, t_size ) type(*), intent(inout), target :: t(..) integer, intent(in) :: t_size blk: block character(len=t_size), pointer :: ptr_s => null() call c_f_pointer( cptr=c_loc(t), fptr=ptr_s ) ptr_s = transfer( source=s, mold=ptr_s ) ptr_s => null() end block blk return end subroutine get_s end module m
program p use m, only : get_s integer, parameter :: lens = 12 character(len=:), allocatable :: s !character(len=lens) :: s s = repeat( "", ncopies=lens ) call get_s( s, len(s) ) print *, "s = ", s stop end program p
C:\..>ifort /c /standard-semantics /traceback /warn:all /check:all m.f90 Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R ) 64, Version 17.0.1.143 Build 20161005 Copyright (C) 1985-2016 Intel Corporation. All rights reserved. C:\..>ifort /c /standard-semantics /traceback /warn:all /check:all p.f90 Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R ) 64, Version 17.0.1.143 Build 20161005 Copyright (C) 1985-2016 Intel Corporation. All rights reserved. C:\..>link /out:p.exe /subsystem:console p.obj m.obj Microsoft (R) Incremental Linker Version 14.00.24215.1 Copyright (C) Microsoft Corporation. All rights reserved. C:\..>p.exe s = forrtl: severe (157): Program Exception - access violation Image PC Routine Line Source p.exe 000000013FC46DE0 Unknown Unknown Unknown p.exe 000000013FC097C7 Unknown Unknown Unknown p.exe 000000013FC0143A MAIN__ 12 p.f90 p.exe 000000013FC4F3CE Unknown Unknown Unknown p.exe 000000013FC4FD5D Unknown Unknown Unknown kernel32.dll 0000000076E159CD Unknown Unknown Unknown ntdll.dll 000000007704A561 Unknown Unknown Unknown
P.S.> a typo (change use s_m to use m) in main program p corrected per Message #2