> Lokesh Chugh, Noida wrote:
>
> > "A derived type with the BIND attribute shall not have components
with
> > default initialization."
> It seems there isn't in the published standard.
Yes, it is not in the published standard.
It was just my query whether standard should have something like above !
> > I am asking this query, as C standard does not support a feature
that is
> > equivalent to default initialization for derived types in FORTRAN.
> Yes, but this is not a necessary criterion for interoperability---C
does
> not have optional arguments yet recently we added them to Interop for
> the next revision. I would say a relevant criterion is whether there
is
> an actual problem with having default initialization? Certainly it is
> useful if the bulk of codes is written in Fortran---why not use the
> feature. It does not change the physical representation of the type
and
> C can use the values regardless of whether they were (partially)
> produced by default initialization.
>
> In Fortran, a major semantical guideline is that a thingo of derived
> type with default initialization cannot (ever!) end up
undefined---that
> is, instead of becoming undefined (as it would if the type did not
have
> default initialization), it is default (re)initialized. So this is
where
> I can see a possible problem occuring, but I have not thought enough
> about it. Do you see an actual problem (implementation or semantics
wise)?
Thanks Alek. I understand your explanation.
To the real time problem, please consider the following FORTRAN and C
sources that interoperate:
FORTRAN program:
---------------
Module mod1
type ,bind(c)::der
integer(kind=4)::a=10
integer(kind=4)::b=20
end type der
type (der),bind(c,name="c_der")::obj
end module mod1
Program Main
use mod1
implicit none
write(*,'(1A45)')"Initialization of derived type from fortan"
write (*,'(1I10)') obj%a
write (*,'(1I10)') obj%b
write(*,'(1A35)')"c function is invoked "
call sub()
write(*,'(1A35)')"Now they are "
write (*,'(1I10)') obj%a
write (*,'(1I10)') obj%b
write(*,'(1A45)')"==========================================="
End
C program:
-----------
extern struct{
int a;
int b;
}c_der; /** Cannot be default initialized **/
void sub_(){
c_der.a=9;
c_der.b=12;
}
Since TYPE definition in FORTRAN and struct in C cannot be considered to
be same because one contains default initialization and other does not.
So, there is an ambiguity to predict output in such cases.
What should be the output in above case at "Initialization of derived
type from fortan"?
Should it be:
===========================================
Initialization of derived type from fortan
0
0
OR
===========================================
Initialization of derived type from fortan
10
20
I could not figure out expected output from F2003 standard in this case.
I am even afraid that different compiler vendors can interpret above
situation differently.
I have confirmed F2003 standard, it is silent about default
initialization for interoperating derived type.
There can be another situation when "extern" is removed from C-struct
variable.
Please let me know if there is any doubt in my explanation.
Thanks and best regards,
Lokesh
"To have good picture, set the focus right"
|