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

ALLOCATED with scalar pointer

cniggeler
Beginner
199 Views

Hi, I am trying to understand ALLOCATED(), ASSOCIATED(), and the Fortran rules surrounding pointer objects. Different compilers seem to produce different results, which is distressing; maybe because of how the spec is interpreted? I created this code:

 

character(len=128), pointer :: pname
allocate(pname)
pname = "Testing"
print *, "Print2d1 ", pname
print *, "Print2d2 ", allocated(pname), associated(pname)

 

and ifort and ifx output the following:

 

cwe476-5.f90(63): error #7397: The argument of the ALLOCATED inquiry intrinsic function should have the ALLOCATABLE attribute.   [PNAME]
   print *, "Print2d2 ", allocated(pname), associated(pname)
-----------------------------------^
cwe476-5.f90(63): error #6547: The ARRAY argument of the ALLOCATED inquiry intrinsic function shall be an allocatable array.   [PNAME]
   print *, "Print2d2 ", allocated(pname), associated(pname)
-----------------------------------^

 

  1.  I thought pointers were defacto allocatable?
  2.  Section 13.7.9 of the F08 spec states that ALLOCATED() is applicable to an array or scalar.

On top of that, if I simply drop line 5 above (the print* containing ALLOCATED()), ifort compiles without complaint and outputs this:

 

 Print2d1
 Testing

 

so it seems pname was allocated (and thus is allocatable) and an anonymous target was created?

It's also important to note that, if I leave out "allocate(pname)", ifort compiles but aborts with "Program Exception - access violation", again seeming to confirm that pname is allocatable and indeed was allocated.

So, why does ifort raise the error messages? In contrast, ftn95 compiles the original code at top with a warning about PNAME not being allocatable, and produces this:

 

 Print2d1 Testing

 Print2d2   T  T

 

 Thank you!

0 Kudos
1 Solution
Mark_Lewy
Valued Contributor I
178 Views

Your assumption that pointers were defacto allocatable is incorrect.  While you can ALLOCATE a pointer, you cannot use the ALLOCATED intrinsic on a pointer.

Actual arguments you pass to ALLOCATED must have the ALLOCATABLE attribute.

Similarly, ASSOCIATED expects arguments to have the POINTER attribute.

 

View solution in original post

2 Replies
Mark_Lewy
Valued Contributor I
179 Views

Your assumption that pointers were defacto allocatable is incorrect.  While you can ALLOCATE a pointer, you cannot use the ALLOCATED intrinsic on a pointer.

Actual arguments you pass to ALLOCATED must have the ALLOCATABLE attribute.

Similarly, ASSOCIATED expects arguments to have the POINTER attribute.

 

FortranFan
Honored Contributor II
117 Views

@cniggeler ,

You may also want to review the Fortran Discourse site at Fortran Discourse - Fortran open source community (fortran-lang.discourse.group) for some discussions around objects of `POINTER` attribute and other aspects around Fortran.

Separately, you may want to note much water has flown under the bridge between when your `ftn95` was last updated and now.  The current Intel Fortran compiler strives to conform to Fortran 2018 standard with increasing levels of Fortran 2023 being added with each revision.  Thus you may want to refer to the current Fortran standard and its semantics to relate to Intel Fortran behavior.

0 Kudos
Reply