[Info-vax] Possible C compiler static variable initialization problem.
glen herrmannsfeldt
gah at ugcs.caltech.edu
Wed Feb 26 16:00:48 EST 2014
Simon Clubley <clubley at remove_me.eisner.decus.org-earth.ufp> wrote:
(snip)
> I'm now really curious about this now I think I've remembered a bit more.
> IIRC, on the C compiler in question you only had one declaration allowed
> for a variable at file level and all the other file level declarations
> in other source files _had_ to be extern references to the first
> declaration.
> After thinking about it a bit more, I cannot be sure anymore, if you tried
> to define the variable twice at file level in different source files, if
> the linker wrongly allocated additional space for the second variable
> (like I stated above) or if it issued a multiple allocation error.
>
> Anyone ever worked with a compiler like this and remember the compiler
> and operating system ?
Strangely, this problem just came up in comp.lang.fortran, when
calling C from Fortran. Specifically, the C variable was extern,
and the Fortran variable the Fortran equivalent, such that the linker
claimed it was undefined.
> PS: I've just tried it on Linux and binutils merged the two definitions
> for "unsigned long int a;" in different source files together without
> error just as expected and my test program treated them as one variable.
C requires static variables, including external ones, to be initialized
to zero if not another value. It would seem easy for linkers to detect
initializing a variable more than once, but many don't.
Try it with two definitions with different values, and see which value
stays. (The OS/360 and successors define that the first value is used.
That is one way they do actual linkage editing. You can replace an
existing external value, including an entry name, by defining it
earlier in the input.)
-- glen
More information about the Info-vax
mailing list