[Info-vax] Possible C compiler static variable initialization problem.
Joukj
joukj at hrem.nano.tudelft.nl
Fri Feb 28 04:05:49 EST 2014
glen herrmannsfeldt wrote:
> The Fortran rules are slightly different, in that you can have
> any number of COMMON blocks specified (which don't initialize to
> a specific value) and at most one in BLOCK DATA which can initialize
> the value.
The COMMON block is nothing more than a pointer to a heap of statically
allocated memory that is mapped in each subroutine to a list of
variables. The mapping may vary within a program for subroutine to
subroutine. (Handy in the "old" days when RAM was expensive)
In more modern Fortran (F90) one can define modules containing variables
which are "global" to the subroutines that include the module. Compilers
normally warn when there is a name clash between a locally defined
variable and one in a module (that is why you always should use
"implicit none"), or between 2 models used in the same subroutine.
2 Modules containing the same variable but never used together in one
subroutine are just 2 different globals: The compiler just includes the
name of the module in the varialble name: i.e. for OpenVMS
$<modulename$<vriablename>. For the linker these variables are different.
>
> More interesting when you mix Fortran and C.
>
With the knowledge above one could do on OpenVMS
$ ty test.f90
module glob
implicit none
integer glob1
end module glob
program test
!
use glob
!
implicit none
!
call c_glob
write(*,*) glob1
!
end
$ ty c_glob.c
extern int $glob$glob1;
void c_glob()
{
$glob$glob1 = 200;
return;
}
$ f90 test.f90
$ cc c_glob.c
$ link test,c_glob
$ run test
200
$
Probably this code is not portable to any other platform than OpenVMS.
The name of the global variable in the C code has to be defined differently.
Jouk
More information about the Info-vax
mailing list