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

possible bug with c_ptr

talziary
Beginner
824 Views
Hi everybody,

I have problems when tryng to compile the GTK-Fortran interface defined by Jerry Delisle and Vincent Magnin (see https://gitub.com/jerryd/gtk-fortran) with ifort 12.1.4 on Fedora fc16.
The problems come from all the Fortran functions that should return a c_ptr pointer and use it before leaving the function : the pointer returned by the function has a wrong value . The result are ok with gfortran.

As it is too difficult to look inside the GTK-Fortran interface I have reproduced the problem in a small test program (see attached files)
Here is an example of the function which cause problem :
[bash]!! A high-level function which : !! - call def_point to define the pointer point !! - call set_value in order to put ival in the location pointed by point !! This high-level function define the pointer and use it before exiting function hl_def_point(ival) result(point) use mymod use iso_c_binding, only: c_ptr,c_int implicit none integer(c_int),intent(in)::ival type(c_ptr)::point integer(c_int)::ier ! *****define the pointer point point=def_point() ! *****we use the pointer point before exiting ier=set_value(point,ival) end function hl_def_point[/bash] Curiously the problem can be overcome by definig and using a local c_ptr pointer and defining the function value only at the end just before leaving the function :
[bash]!! A high-level function which : !! - call def_point to define the pointer point !! - call set_value in order to put ival in the location pointed by point !! This high-level function defines a local pointer, uses it, and !! defines the resulting pointer only just before exiting function hl_def_point(ival) result(point) use mymod use iso_c_binding, only: c_ptr,c_int implicit none integer(c_int),intent(in)::ival type(c_ptr)::mypoint,point integer(c_int)::ier ! *****define the pointer mypoint mypoint=def_point() ! *****we use the pointer mypoint before exiting ier=set_value(mypoint,ival) point=mypoint end function hl_def_point[/bash] However I don't really understand why this trick works !

This looks like a rather subtle bug about c_ptr

0 Kudos
3 Replies
Steven_L_Intel1
Employee
824 Views
I am fairly certain that this is our issue ID DPD200179339 in which the compiler does not properly pass by value nor receive as function return values consisting of a "small" structure when the called routine is declared as BIND(C). This is fixed for a release later this year. I need to verify this.

A workaround might be to declare the return type as an INTGER(C_INTPTR_T) and use TRANSFER to convert it to a C_PTR.
0 Kudos
talziary
Beginner
824 Views
Thank you Steve,

For the time being I prefer to use my trick of defining a local c_ptr pointer inside the function because it avoids to change the type of the the function and to search all the locations where it is called. The Gtk-Fortran interface is a rather big stuff which involves many functions of this kind !

Is it possible to see somewhere the list and the definition of the current issues like ID DPD2001179339 ?

Hope that the new release will come soon

Many thanks for your help

talziary
0 Kudos
Steven_L_Intel1
Employee
824 Views
For the definition, see here.
0 Kudos
Reply