Manipulating array descriptors from C?

The descriptor passed to fcbind is only valid for the duration of the call. If you compile your program with -fdump-tree-original and inspect the resulting file, you’ll see the problem:

    {
      void * cfi.7;    // <-- temporary descriptor created

      if ((real(kind=4)[0:] * restrict) a.data == 0B)
        {
          a.dtype = {.elem_len=4, .rank=1, .type=3};
        }
      a.span = (integer(kind=8)) a.dtype.elem_len;
      a.dtype.attribute = 1;
      cfi.7 = 0B;
      _gfortran_gfc_desc_to_cfi_desc (&cfi.7, &a);
      a.dtype.attribute = 1;
      fcbind (&h, cfi.7);
      _gfortran_cfi_desc_to_gfc_desc (&a, &cfi.7);
      __builtin_free (cfi.7);  // <-- destroys the descriptor stored in the C struct,
                               //     making it corrupt
    }
    {
      static integer(kind=4) C.3918 = -5;

      fcsetlb_h (&h, &C.3918);
    }

In the print statement afterward, the lbound() function uses the original gfortran descriptor of a. The handle however is corrupt.