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

iso_c_binding c_loc error

Dong_Zheng
Beginner
554 Views
There is an error in function c_loc in iso_c_binding module.

For ifort 2011.5.220,

nullify(fi)
a = c_loc(fi(1))

does not return a NULL toc. It works fine on ifort 11.0.081

Test files are attached
0 Kudos
3 Replies
Steven_L_Intel1
Employee
554 Views
Your program is not legal. Quoting the standard, in its description of the argument to C_LOC: "If it is a pointer, it shall be associated". It is also not legal to reference fi(1) when fi is disassociated. The result of running this program is undefined.
0 Kudos
Dong_Zheng
Beginner
554 Views
Thank. Now I got it. I changed all C_LOC calls in my code from

a = C_LOC(fi(1)

to

if (associated(fi)) then
a = C_LOC(fi(1))
else
a = C_NULL_PTR
endif

Then, it works fiine.

In my real code, the "nullify"s and "allocate"s are done somewhere else then C_LOC. So, I have to add the if statement to check if the fortran pointer is associated. In this sense, It would be easier to program if C_LOC can convert null fortran pointer to NULL C pointer although it is not the standard.

Thanks again.

Dong Zheng
0 Kudos
Steven_L_Intel1
Employee
554 Views
If you said C_LOC(fi) you would probably get the result you want, though this is still not legal Fortran. But what you have written is better overall.
0 Kudos
Reply