- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
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.
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The code does not conform, but the compiler is not required to diagnose this.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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).
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).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It does indeed seem to be standard compliant without needing the BIND(C) in light of the standard quoted. Thanks.

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page