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

Another -e03 question with derived data types and C

breitenfeld
Beginner
782 Views
I have the following program

PROGRAM main
USE ISO_C_BINDING
TYPE :: ir
INTEGER(C_INT) :: int_scalar
REAL(C_FLOAT) :: real_scalar
END TYPE ir
TYPE(ir), TARGET :: derivedtype
TYPE(C_PTR) :: f_ptr
f_ptr = C_LOC(derivedtype)
END PROGRAM main

and if I compile with -e03 it does not give any warnings. I thought for a derived type to be interoperable as defined by the standard it has to have a BIND(C)

TYPE, BIND(C) :: ir

What am I missing, does this code without the bind(C) conform to the standard? Or is this another situation where "don't have a good way of enforcing the standard's requirement of an interoperable type."

In other words, the -e03 flag in general will not check if X in C_LOC(X) is interoperable, and does not produce a warning even if X is not interoperable and violates the standard.



0 Kudos
4 Replies
Steven_L_Intel1
Employee
782 Views
The code does not conform, but the compiler is not required to diagnose this.
0 Kudos
reinhold-bader
New Contributor II
782 Views
Hello,

I think that the code, as it stands, is standard conforming. While the derived type is not interoperable, the c_loc() intrinsic does accept scalars of non-interoperable type. This is the mechanism provided to pass a handle to non-interoperable data through C - so if you misuse this handle, this would either happen on the C side of things (on which the compiler has no influence), or because unpacking the handle with c_f_pointer() is performed incorrectly (which the compiler typically cannot diagnose).
0 Kudos
Steven_L_Intel1
Employee
782 Views
On a reread, I think you are correct. The argument to C_LOC is allowed to be "a nonpolymorphic scalar, have no type parameters and be... a nonallocatable, nonpointer variable that has the TARGET attribute", which is the case here.
0 Kudos
breitenfeld
Beginner
782 Views
It does indeed seem to be standard compliant without needing the BIND(C) in light of the standard quoted. Thanks.
0 Kudos
Reply