Silverfrost Forums

Welcome to our forums

ISO_C_BINDING

2 Jun 2021 1:09 #27891

Is this implemented fully?

For example C_F_POINTER doesn't seem to be recognised.

2 Jun 2021 4:41 #27892

StamK

Fortran 2003 ISO_C_BINDING is implemented except for the intrinsics C_ASSOCIATED, C_F_POINTER and C_F_PROCPOINTER.

3 Jun 2021 6:21 #27893

StamK

Do you have code that uses C_F_POINTER. Can you post a sample?

18 Jun 2021 11:59 #27969

Sorry, somehow didn't get a notification of your reply. The code is the one in the accepted answer https://stackoverflow.com/questions/19147743/passing-allocatable-array-from-fortran-to-c-and-malloc-it Was trying to use malloc/free directly since we are having problems with get_storage@ etc.

19 Jun 2021 6:42 #27972

StamK

If you are using only FTN95 then there will probably be a fix that we could provide. Alternatively we may be able to implement C_F_POINTER for you.

Either way we would need some sample code that illustrates what you want to do.

20 Jun 2021 11:06 #27978

We just want to develop our own malloc/free Fortran calls, and iso_c_binding allows to use the C pointers seamlessly. In any case C_F_POINTER is a very important element of iso_c_binding so it is important to get it to work.

20 Jun 2021 11:34 #27979

StamK

Can you post some code that illustrates how C_F_POINTER is used.

20 Jun 2021 12:00 #27981

Courtesy of https://stackoverflow.com/questions/19147743/passing-allocatable-array-from-fortran-to-c-and-malloc-it

#include 'stdlib.h'
int *create_storage()
{
   /* Array of four integers. */
   return malloc(sizeof(int) * 4);
}

void destroy_storage(int *ptr)
{
   free(ptr);
}


PROGRAM fortran_side
  USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR, C_F_POINTER, C_INT
  IMPLICIT NONE
  INTERFACE
    FUNCTION create_storage() BIND(C, NAME='create_storage')
      USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
      IMPLICIT NONE
      TYPE(C_PTR) :: create_storage
    END FUNCTION create_storage
    SUBROUTINE destroy_storage(p) BIND(C, NAME='destroy_storage')
      USE, INTRINSIC :: ISO_C_BINDING, ONLY: C_PTR
      IMPLICIT NONE
      TYPE(C_PTR), INTENT(IN), VALUE :: p
    END SUBROUTINE destroy_storage
  END INTERFACE
  TYPE(C_PTR) :: p
  INTEGER(C_INT), POINTER :: array(:)
  !****
  p = create_storage()
  CALL C_F_POINTER(p, array, [4])   ! 4 is the array size.
  ! Work with array...
  CALL destroy_storage(p)
END PROGRAM fortran_side
21 Jun 2021 9:01 #27983

StamK

This code suggests to me that C_F_POINTER is equivalent to the FTN95 extension of using ABSOLUTE_ADDRESS with ALLOCATE...

ALLOCATE(array(4), ABSOLUTE_ADDRESS=p)

but I wonder why you would want to do this rather than simply call ALLOCATE(array(4)).

Also if GET_STORAGE@ does not work for you then we should look into that.

28 Jun 2021 9:19 #28012

StamK

In the next release of the FTN95 library, the 64 bit definition of GET_STORAGE@ has been improved. You have reported that GET_STORAGE@ was not working for you and this change may make a difference.

This change will be in the next release of the DLLs.

28 Apr 2023 12:07 #30240

Hi, is the implementation of C_ASSOCIATED going to happen any time soon? Thanks

28 Apr 2023 1:46 #30243

StamK

I will put it on the wish list. I can't say when it will be implemented at the moment.

7 May 2023 8:15 #30277

StamK

I have had a look at C_F_POINTER and my initial impression is that it will be a major task to add it to FTN95.

If you are having problems with GET_STORAGE@ then I am sure that it would be a lot quicker for us to fix that issue or for us to provide an alternative direct method for getting memory.

Please provide code that illustrates your problems when getting memory.

7 May 2023 12:57 #30278

If you are writing a program for a Windows operating system then a call to get memory will almost certainly end up with a call to the Windows API function HeapAlloc.

This will be the case, for example, if you call ALLOCATE from Fortran, GET_STORAGE@ from FTN95 or malloc from a program written in the language C.

Here is a simple FTN95 program that illustrates how to directly call HeapAlloc.

PROGRAM main
STDCALL HeapAlloc 'HeapAlloc' (VAL7,VAL3,VAL7):INTEGER(7)
STDCALL GetProcessHeap 'GetProcessHeap':INTEGER(7)
STDCALL HeapFree 'HeapFree' (VAL7,VAL3,VAL7):INTEGER*4
INTEGER(7):: han,addr
INTEGER,PARAMETER:: HEAP_ZERO_MEMORY=8
INTEGER ret
han = GetProcessHeap()
addr = HeapAlloc(han, HEAP_ZERO_MEMORY, 1024_7)
IF(addr > 0) write(*, '(A,2Z8)') ' 1024 bytes of memory allocated:',han,addr
ret = HeapFree(han, 0, addr)
IF(ret /= 0) write(*,*) 'Memory has been de-allocated'
END PROGRAM
Please login to reply.