Quantcast
Channel: Intel® Software - Intel® Visual Fortran Compiler for Windows*
Viewing all articles
Browse latest Browse all 5691

Implicit length character array and bind(c) subroutine

$
0
0

We recently updated to IFORT 15.0.2.179. A few of the regression tests for our code system now fail. The basic pattern is calling a function written in C with a slice of a two-dimensional character array; the compiler is not passing a pointer to the correct word in the character array. Here is a reduced example; we declare an interface to the C routine and iterate over the rows of a character array, passing the first column to the C function:

module r
  interface
     subroutine cprint ( leng, chars ) bind (c)
       use, intrinsic :: iso_c_binding, only : c_int, c_char
       integer(kind=c_int) :: leng
       character(kind=c_char,len=1) :: chars( leng )
     end subroutine cprint
  end interface
end module r

subroutine b ( leng, chars )
  use r
  integer :: leng, i
  character(len=*) :: chars( leng, 2 )
  do i = 1, 2
     call cprint( leng, chars(:,i) )
  end do
end subroutine b

program a
  integer, parameter :: leng = 23
  integer :: i
  character(len=1) :: chars( leng, 2 )
  character(len=leng) :: buffer
  buffer = 'ABCDEFGHIJ'
  do i = 1, leng
     chars(i,1) = buffer(i:i)
  end do
  buffer = '1234567890'
  do i = 1, leng
     chars(i,2) = buffer(i:i)
  end do
  call b( leng, chars )
end program a

and the C++ subroutine:

#include <cstdio>
extern "C" void cprint ( const int* leng, const char* chars )
{
  for ( int i = 0; i < *leng; ++i ) {
    putc( chars[i], stdout );
  }
  putc( '\n', stdout );
  fflush( stdout );
}

I expect this to print:

ABCDEFGHIJ
1234567890

But instead I get:

ABCDEFGHIJ
ABCDEFGHIJ

If I look at the assembly code for subroutine b, it appears that the correct address for chars(:,i) is not being computed; it just uses the same address for each iteration.

Variations: If I replace character(len=*) with character(len=1) in subroutine b, the correct strings are printed by cprint. If I leave in (len=*) but call a Fortran print subroutine with the same arguments, the correct strings are printed.

I tried my example program with ifort 14.0.4.237 and it also prints the incorrect strings. But, our main program, which is lot larger, works correctly. Kind of odd.

Thanks,
Allen


Viewing all articles
Browse latest Browse all 5691

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>