Silverfrost Forums

Welcome to our forums

Workaround for C_F_POINTER

27 Feb 2025 11:35 #31951

Paul Thanks for providing me with a working example. It allowed me to create a reproducer for error 1251 at compile time using FTN95 v.9.10 x64. The reproducer compiles with ifort.

program main
  use iso_c_binding,only:c_ptr,c_f_pointer
  implicit none
  interface
    subroutine c_f_pointer_test$(q) bind(c,name='__c_f_pointer_test')
      import :: c_ptr
      type(c_ptr), intent(out) :: q
    end subroutine
  end interface
  type(c_ptr) :: cptr
  REAL :: X
  
  call c_f_pointer_test$(cptr)
  X=TEST(cptr)
  PRINT*, X
  
  CONTAINS
  
  REAL FUNCTION TEST(J)
  implicit none
  type(c_ptr), INTENT(IN) :: J
  real,pointer :: a(:)
  real,pointer :: b(:,:)
  real,pointer :: c
  real::ss1,ss2
  integer s(1)
  s = [12]
  call c_f_pointer(J, a, s)
  ss1 = sum(a)
  call c_f_pointer(J, b, [3,4])
  ss2 = sum(b)
  call c_f_pointer(J, c)
  if(ss1 == ss2 .and. c == 1.0) print*, 'Success'
  TEST=a(12)
  END FUNCTION TEST
  
end program main
27 Feb 2025 11:45 #31953

jlb

Thank you for the bug report. I have logged this for investigation.

11 Mar 2025 1:38 #31990

This failure has now been fixed for the next release of FTN95.

12 Mar 2025 9:48 #31991

Paul

Thank you for your prompt assistance. Is this an interim release or a full release of FTN95?

12 Mar 2025 12:02 #31992

It will probably be an interim release following the full release of the personal edition of FTN95 v9.10.

30 May 2025 9:27 #32133

Paul

Compiling your working example with FTN95 version 9.11 fails now with the following error

error 463 - Invalid characters(s) after SUBROUTINE expression

pointing at

subroutine c_f_pointer_test$(p) bind(c,name='__c_f_pointer_test')
31 May 2025 6:31 #32141

jlb

There is a regression at v9.11 regarding BIND(C...). I will aim to upload a fix shortly.

30 Oct 2025 7:49 #32433

Hi all, I am reaching out to ask if there are any updates regarding support for C_F_POINTER and C_LOC in subroutines. Currently, I am unable to use these features within subroutines, although they seem to work fine in a program context, as shown below:

       program main
       use,intrinsic::iso_c_binding

      implicit none
      
	   interface
      subroutine c_f_pointer_test(p) 
     & bind(c,name='__c_f_pointer_test')
      import :: c_ptr
      type(c_ptr), intent(out) :: p
      end subroutine
      end interface
      
      type(c_ptr) :: cptr
      real,pointer :: a(:)
      real,pointer :: b(:,:)
      real,pointer :: c


      logical L(6)
      
      L(1) = .not.c_associated(cptr)

      call c_f_pointer_test(cptr)      ! Create cptr
      shp = 12
      
      call c_f_pointer(cptr, a, [12])  ! 'a' points to cptr.
      call c_f_pointer(cptr, b, [3,4]) ! 'b' has the same target as 'a' but different shape.
      call c_f_pointer(cptr, c)        ! 'c' is just the first element of the cptr array.
      L(3) = c_associated(cptr)
      L(4) = c_associated(cptr, c_loc(a))
      L(5) = c_associated(cptr, c_loc(b))
      L(6) = c_associated(cptr, c_loc(c))

      if(all(L)) print*, 'Success'
      end program main 

I am currently working with version 9.10. Is there a planned release that will extend support for these features in subroutines?

Please login to reply.