Quantcast
Channel: Fortran
Viewing all articles
Browse latest Browse all 3108

C_NULL_PTR is passed incorrectly with -standard-semantics option

$
0
0

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:

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

and the C code:

#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);
}

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


Viewing all articles
Browse latest Browse all 3108

Trending Articles