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

Malcolm Cohen malcolm at nag-j.co.jp
Mon Oct 7 20:19:53 EDT 2019


You might need an option to make the compiler standard-conforming. Some compilers, at least in the past, did not do (re)allocation on assignment without such an option.

 

Cheers,

-- 

..............Malcolm Cohen, NAG Oxford/Tokyo.

 

From: J3 <j3-bounces at mailman.j3-fortran.org> On Behalf Of Vipul Parekh via J3
Sent: Tuesday, October 8, 2019 9:04 AM
To: General J3 interest list <j3 at mailman.j3-fortran.org>
Cc: Vipul Parekh <parekhvs at gmail.com>
Subject: Re: [J3] Allocatation upon assignment of a dummy argument in an interoperable procedure

 

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
contains
subroutine Fsub( dat ) bind(C, name="Fsub")
!.. Argument list
integer(c_int), allocatable, intent(out) :: dat
!dat = 42 !<-- A
allocate( dat ) ; dat = 42 !<--- B
return
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_attribute_allocatable,
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,
Vipul



Disclaimer

The Numerical Algorithms Group Ltd is a company registered in England and Wales with company number 1249803. The registered office is: Wilkinson House, Jordan Hill Road, Oxford OX2 8DR, United Kingdom. Please see our Privacy Notice <https://www.nag.co.uk/content/privacy-notice>  for information on how we process personal data and for details of how to stop or limit communications from us.

This e-mail has been scanned for all viruses and malware, and may have been automatically archived by Mimecast Ltd, an innovator in Software as a Service (SaaS) for business.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mailman.j3-fortran.org/pipermail/j3/attachments/20191008/ff6956d1/attachment.html>


More information about the J3 mailing list