Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!dciem!nrcaer!cognos!jimp From: jimp@cognos.uucp (Jim Patterson) Newsgroups: comp.lang.c Subject: Re: portability and standards Message-ID: <1068@aldebaran.UUCP> Date: Tue, 7-Jul-87 11:19:23 EDT Article-I.D.: aldebara.1068 Posted: Tue Jul 7 11:19:23 1987 Date-Received: Thu, 9-Jul-87 02:09:48 EDT References: <8113@brl-adm.ARPA> Reply-To: jimp@cognos.UUCP (Jim Patterson) Organization: Cognos Inc., Ottawa, Canada Lines: 56 In article <8113@brl-adm.ARPA> edstrom%UNCAEDU.BITNET@wiscvm.wisc.EDU writes: >I don't understand the need for macros in defining and initializing global >variables. Maybe I am missing the point but what I do for header files with >variables shared by many .c modules is: > >headerfile.h > >extern double example; >double example = 33.3; > >I use this on VAX VMS and it works fine. Is ther some reason why this approach >is not "safe" or "proper"? A good explanation of the data models supported in various C implementations can be found in the ANSI C Rationale document which was distributed with the ANSI C (X3J11) draft documents, in the section 3.1.2.2, Linkage of Identifiers. The same section in the ANSI C draft explains the rules that ANSI has actually adapted. VAX C adapts a style of externs called the Common model. All objects with external scope are placed in what the linker refers to as PSECTs; they are equivalent to FORTRAN NAMED COMMON blocks. This model is very unrestrictive; it effectively ignores the extern keyword and allows any number of initializations of a given external object. The only time you might see a complaint is when the same external object is initialized to two distinctly different and non-zero values. A large number of other C implementations adapt what is refered to as the REF/DEF (or Reference/Definition) model. In this model, an object with external scope may have any number of references identified by explicit use of the extern keyword (and no initializer), but can have only one definition (no extern keyword and an optional initializer). This model is atually termed the Strict REF/DEF model and has been adapted by the ANSI C committee on the grounds that it will break the fewest implementations. A Relaxed REF/DEF model is used by some C implementations as well which allows for multiple definitions but still distinguish between references and definitions based on the extern keyword. Some compilers rely on the presence or absence of initializers to determine which declaration forms the definition of an object. This model is termed the Initialization model. So, to answer your query, your approach isn't portable and isn't supported by the ANSI definition. (This isn't to say that DEC won't continue to support it; I suspect that they will). The Common model is supported by a number of implementations, but isn't supported by many others. ANSI C has adapted REF/DEF because it is compatible with the majority of existing implementations. If you want to be maximally portable, this seems to be the best approach. Also you should include initializers on each definition if you need to be able to port to implementations using the Initialization model. -- Jim Patterson decvax!utzoo!dciem!nrcaer!cognos!jimp Cognos Incorporated