[J3] Allocatation upon assignment of a dummy argument in an interoperable procedure

Vipul Parekh parekhvs at gmail.com
Mon Oct 7 20:03:30 EDT 2019

Bill, Bob:

Re: "it is usually considered poor programming style to use the same
name for a dummy argument and the binding label for the routine,"
thanks, good point - chalk that up to another typo in my original
note; I was in a hurry to send out before I left for a long meeting -
the actual code didn't have that poor style!

Re: "It would be useful to see the C code that sets up the descriptor"
and Bob's questions, "How does it fail?   Was the C descriptor
initialized by CFI_establish and then passed changed to the
interoperable subroutine, or was the C descriptor changed after it was
established?," please see below.

Here's the code that works:

--- begin Fortran module ----
module m
   use, intrinsic :: iso_c_binding, only : c_int
   subroutine Fsub( dat ) bind(C, name="Fsub")
      !.. Argument list
      integer(c_int), allocatable, intent(out) :: dat
      !dat = 42          !<-- A
      allocate( dat ) ; dat = 42      !<--- B
   end subroutine
end module m
--- end Fortran module ---

and the C code:

--- begin C main ---
#include <stdlib.h>
#include <stdio.h>
#include "ISO_Fortran_binding.h"

// Prototype for Fortran functions
extern void Fsub(CFI_cdesc_t *);

int main()

   CFI_CDESC_T(0) dat;
   int irc = 0;

   irc = CFI_establish((CFI_cdesc_t *)&dat, NULL,
      CFI_type_int, 0, (CFI_rank_t)0, NULL);
   if (irc == CFI_SUCCESS) {
      printf("CFI_establish succeeded for dat.\n");
   else {
      printf("CFI_establish failed: irc = %d.\n", irc);
      return EXIT_FAILURE;

   Fsub((CFI_cdesc_t *)&dat);
   printf("Fsub returned: %d\n", *(int *)dat.base_addr);

   irc = CFI_deallocate((CFI_cdesc_t *)&dat);
   if (irc == CFI_SUCCESS) {
      printf("CFI_deallocate for dat succeeded.\n");
   else {
      printf("CFI_deallocate for dat failed: irc = %d.\n", irc);
      return EXIT_FAILURE;

   return EXIT_SUCCESS;

--- end C main ---

Upon execution, the program output is:
CFI_establish succeeded for dat.
Fsub returned: 42
CFI_deallocate for dat succeeded.

Now if the line marked B (i.e., 'allocate( dat ) ; dat = 42') is
commented out and the one marked A is uncommented, the program prints
the first line "CFI_establish succeeded for dat" and the system then
issues a message the program "has stopped working."  The debugger
throws an exception at "dat = 42" line.  My hunch is this is a
compiler issue but first wanted to confirm whether the code conforms
to the standard.

Hope this helps,

More information about the J3 mailing list