4 byte address returned into c_ptr

Seen with pgi 16.5 compilers on 64-bit Linux.

The example below shows that an 8-byte address in C seems to be truncated to 4-bytes when returned to Fortran as a c_ptr. The problem seems to be with pgf90, since:

pgcc + gfortran works
gcc + gfortran works
pgcc + pgf90 fails
gcc +pgf90 fails

I’d appreciate it if someone could take a look.

Compilation and execution dialogue:

> gcc -c main.c
> gfortran isoc.F90 main.o
> ./a.out 
loc(fp(1)) in memory_allocate         7F394F2F3010
allocated at 7f394f2f3010
assigned 999.000000

> pgcc -c main.c
> pgf90 isoc.F90 main.o
isoc.F90:
> ./a.out 
loc(fp(1)) in memory_allocate         7F139382E020
allocated at 9382e020
Segmentation fault

main.c:

#include <stdio.h>
double* memory_allocate();
void cmain() {
  double* dd;
  dd=memory_allocate();
  fprintf(stderr,"allocated at %lx\n",dd);
  dd[0]=999;
  fprintf(stderr,"assigned %f\n",dd[0]);
}

isoc.F90:

program main
  use iso_c_binding
  interface 
  subroutine cmain() bind(C,name="cmain")
  end subroutine cmain
 end interface
 call cmain
end program main
FUNCTION memory_allocate() BIND(C,name="memory_allocate")
 USE iso_c_binding
 TYPE(c_ptr) :: memory_allocate
 CHARACTER(len=1), DIMENSION(:), POINTER :: fp
 allocate(fp(10000000))
 memory_allocate = c_LOC(fp(1))
 write (6,'(A,Z20)') 'loc(fp(1)) in memory_allocate ',loc(fp(1))
 call flush(6)
END FUNCTION memory_allocate

Thanks for the report. It looks like were returning the C_PTR value from the function in an “eax” 32-bit register which is causing the error. I’ve added a problem report (TPR#22709 - Using “C_PTR” as a return type returns pointer in eax (32-bit) register instead of rax) and sent it on to engineering.

  • Mat