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

iso_c_binding and indefinite number of arguments

Alfredo
Beginner
471 Views

Hello,

I am trying to call a C subroutine with an indefinite number of arguments from a Fortran code. I have written something that work ok when using gcc/gfortran (various versions) but I get a segfault with icc/ifort. Here is a simplified version of my code which reproduces the segfault with intel compiler v14 and v15_beta.

The C routine

#include <stdarg.h>
#include <stdio.h>

void va(int in, ...)
{

  int *out;

  printf("The input  value is: %2d\n",in);
  
  va_list varg_list;
  
  va_start(varg_list, in);
  out = va_arg(varg_list, int*);
  *out = in;
  va_end(varg_list);

}
  

The Fortran calling code

program vaf
  use iso_c_binding
  integer(c_int), target  :: in, out

  interface
     subroutine va(in, out) bind(C)
       use iso_c_binding
       integer(c_int), value :: in
       type(c_ptr), value :: out
     end subroutine va
  end interface

  in = 15

  call va(in, c_loc(out))
  write(*,'("The output value is: ",i2)')out
  stop
end program vaf

I don't really know whether this code is compliant or not because I couldn't find much info about this topic on reference books nor on the net. Therefore I can't really tell whether this is a compiler bug or just a broken code. Can anybody help?

Thanks,

Alfredo

 

0 Kudos
1 Solution
mecej4
Honored Contributor III
471 Views

See section 15.3.7 of the Fortran 2008 standard (or see 15.2.6 of the Fortran 2003 standard):

"NOTE 15.20
The C language allows specification of a C function that can take a variable number of arguments (ISO/IEC
9899:1999, 7.15). This part of ISO/IEC 1539 does not provide a mechanism for Fortran procedures to
interoperate with such C functions."

View solution in original post

0 Kudos
2 Replies
mecej4
Honored Contributor III
472 Views

See section 15.3.7 of the Fortran 2008 standard (or see 15.2.6 of the Fortran 2003 standard):

"NOTE 15.20
The C language allows specification of a C function that can take a variable number of arguments (ISO/IEC
9899:1999, 7.15). This part of ISO/IEC 1539 does not provide a mechanism for Fortran procedures to
interoperate with such C functions."

0 Kudos
Alfredo
Beginner
471 Views

Mecej4,

thanks for your prompt response. This definitely answers the question.

Kind regards,

Alfredo

 

0 Kudos
Reply