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

C_NULL_PTR is passed incorrectly with -standard-semantics option

Neil_Carlson
Einsteiger
1.086Aufrufe

The following example calls a C function with a void* argument, first passing an associated TYPE(C_PTR) actual, and then passing C_NULL_PTR.  When compiled with ifort 13.1.2 and the -standard-semantics option, the example executes as expected, but with 14.0.1 the C function is receiving a non NULL pointer in the latter case, which is incorrect.  Note that the example runs correctly when the -standard-semantics option is omitted.

I'll attach the C and Fortran source files, but here is the Fortran code:

[fortran]program main

  use :: iso_c_binding

  interface

    subroutine c_function(arg) bind(c)

      import c_ptr

      type(c_ptr), value :: arg

    end subroutine

  end interface

  integer, target :: arg = 42

  print *, 'passing c_loc(arg) to C_function'

  call c_function (c_loc(arg))

  print *, 'passing c_null_ptr to C_function'

  call c_function (c_null_ptr)

end program[/fortran]

and the C code:

[cpp]#include <stdio.h>

void c_function (void *arg) {

  if (arg == NULL)

    printf("\\targ pointer is NULL in c_function\\n");

  else

    printf("\\targ pointer is not NULL in c_function; arg=%d\\n",*(int*)arg);

}[/cpp]

The (correct) output using the 13.1.2 compiler is:

passing c_loc(arg) to C_function

arg pointer is not NULL in c_function; arg=42

passing c_null_ptr to C_function

arg pointer is NULL in c_function <=== THIS IS RIGHT

The incorrect output using the 14.0.1 compiler is:

passing c_loc(arg) to C_function

arg pointer is not NULL in c_function; arg=42

passing c_null_ptr to C_function

arg pointer is not NULL in c_function; arg=0  <=== THIS IS WRONG

0 Kudos
4 Antworten
Steven_L_Intel1
Mitarbeiter
1.086Aufrufe

This should be fixed in 14.0.2, which I expect to be available Wednesday or Thursday this week.

FortranFan
Geehrter Beitragender III
1.086Aufrufe

Steve,

Is it possible for you to explain how the -standard_semantics option adversely affected the behavior in 14.0.1?  I'm curious since the code posted appears to use standard Fortran 2003 stuff for C-interoperability.  Hence I would have thought including or omitting the -standard_semantics option would make no difference.

Thanks,

 

Steven_L_Intel1
Mitarbeiter
1.086Aufrufe

It was related to the treatment of VALUE for non-BIND(C) routines. We had misimplemented this back in 10.0 causing it to act like ATTRIBUTES VALUE, where the standard says to make a copy. But when we tried to fix this late in the 14.0 beta cycle, it caused problems elsewhere, so we came up with -assume std_value and made this part of -standard-semantics.

The problem here was that pass-by-value for "small structures" requires special treatment and we missed making the appropriate changes for that case, when we backed out the original fix, resulting in the inconsistency noted here.

Steven_L_Intel1
Mitarbeiter
1.086Aufrufe

Update 2 is now available at the Intel Registration Center.

Antworten