Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site brl-tgr.ARPA Path: utzoo!watmath!clyde!burl!ulysses!gatech!seismo!brl-tgr!gwyn From: gwyn@brl-tgr.ARPA (Doug Gwyn ) Newsgroups: net.lang.c,net.bugs.usg Subject: Re: cc bug in definition of global variables? Message-ID: <3381@brl-tgr.ARPA> Date: Tue, 19-Nov-85 08:46:52 EST Article-I.D.: brl-tgr.3381 Posted: Tue Nov 19 08:46:52 1985 Date-Received: Thu, 21-Nov-85 04:05:17 EST References: <819@asgb.UUCP> Organization: Ballistic Research Lab Lines: 44 Xref: watmath net.lang.c:7109 net.bugs.usg:375 > The following seems to be a bug in the SYSV.2 C-compiler (loader really): > You can DEFINE the same global variable in two files and the loader does > not even issue a warning about it. In fact, you can DEFINE the same global > variable in two files as two DIFFERENT data types and the loader does not > issue a warning as long as the size of the two globals is the same. Also, > it really treats them as one global variable. Thus, the effect is the same > as declaring the global as the union of the two declarations (as an example, > try the program at the end of this message). > > This behavior of the compiler is contrary to what is mentioned in K&R. > To quote, (pp 77, italicized words from the text are shown in upper case): > " There must be only one DEFINITION of an external variable > ^^^^^^^^^^^^^^^^^^^^^^^^^^^ > among all the files that make up the source program; others > may contain 'extern' declarations to access it." > Also, on page 206 (Appendix A): > "Thus in a multi-file program, an external data definition without > the 'extern' specifier must appear in exactly one of the files." > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ This is not a bug. Although Ritchie preferred the COMMON model for extern data, which is what most UNIX PCCs implement, there are some systems supporting C that have unacceptable restrictions on COMMON data (such as, minimum space 4Kb each, limited quantity available, etc.). Therefore the official language description is in terms of the DEF/REF model of external data linkage, which has no such limitations on those systems. If you write your code according to the (DEF/REF) rules, it will also work correctly on systems using COMMON for extern data, but not vice-versa. There is a story that some early release of USG UNIX (5.0?) had been changed to enforce DEF/REF semantics, and so much code broke that they had to back out the change (by providing "mcc", or "cc -m", or some such temporary scheme) to still the outcries. I heard that some AT&T internal sites refused to install the changed SGS until extern data linkage was put back the way it used to be. If there is an interesting story here, perhaps someone involved will tell us. Note that if you had tried initializing the multiple definitions of extern data with different values, the loader would have issued a warning. But there is a special kludge in "ld" that allows multiple definitions so long as they are all (uninitialized) common except for at most one (initialized) .data definition. (The sizes also are maxed together, as I recall.)