Correct or Not or Old-fashioned or Bug

Zhou Mi zhoumi at nff.ncl.omron.co.jp
Thu May 23 11:25:19 AEST 1991


	Up to now, I've got many answers. Here, I give my thanks to all the
people who gives me the answer by E-mail or by News.

	But, there are so many conflicting answers that I feel really 
confusion. Can anyone give me a proper conclusion or answer ??

	First all, I give my working environment here:
        I'm working on :
                machine: OMRON HOLONIC WORKSTATION SX9100/DT
                OS:     UNIOS-B 4.3BSD UNIX: 1.60Beta
                Compiler: both cc and gcc

	There are some of the answers , which I've get, as follows:

1)
Date: Mon, 20 May 91 09:02:17 PDT
From: raymond at math.berkeley.edu (Raymond Chen)
Message-Id: <9105201602.AA07902 at math.berkeley.edu>
Received: by sunkist.berkeley.edu (4.1/1.31)
        id AA03310; Mon, 20 May 91 09:02:16 PDT
To: zhoumi at nff.ncl.omron (Zhou Mi)
Subject: Re: Correct or Not or Old-fashioned or Bug
Newsgroups: comp.lang.c
In-Reply-To: <ZHOUMI.91May20182038 at marlboro.nff.ncl.omron.co.jp>
Organization: U.C. Berkeley
Status: RO

Although it is incorrect(even by the old K&R standard), it works on
unix (and only on unix) systems because of a bug in the linker.

>From raymond at math.berkeley.edu Wed May 22 03:18:37 1991
Received: by othello.nff.ncl.omron.co.jp (5.65-omron/6.3J-omron)
        id AA06141; Wed, 22 May 91 03:18:31 +0900
Received: from math.Berkeley.EDU by uunet.uu.net with SMTP
        (5.61/UUNET-uucp-primary) id AA01257; Tue, 21 May 91 11:06:58 -0400
Received: from sunkist.berkeley.edu by math.berkeley.edu (4.1/1.33(math))
        id AA13916; Tue, 21 May 91 08:06:38 PDT
Date: Tue, 21 May 91 08:06:38 PDT
From: raymond at math.berkeley.edu (Raymond Chen)
Message-Id: <9105211506.AA13916 at math.berkeley.edu>
To: zhoumi at nff.ncl.omron
Subject: Re: Correct or Not or Old-fashioned or Bug
Status: RO

|Why is the BUG still alive ??

Because the BUG is a hack used by the compiler/linker to control
whether or not to link stdio and/or floating point.

2)
Date: Tue, 21 May 91 15:05:55 GMT
From: steve at taumet.com (Stephen Clamage)
X-Local-Time: Tue, 21 May 91 08:05:55 PDT
To: zhoumi at nff.ncl.omron
Subject: Re: Correct or Not or Old-fashioned or Bug
Newsgroups: comp.lang.c
References: <ZHOUMI.91May20182038 at marlboro.nff.ncl.omron.co.jp>
Status: RO

In comp.lang.c you write:

>When I am working in a project, I find that someone in my group write
>the following program. Though it seems work well, I still wonder if it
>is correct or better ??

[ 'int i;' included in all modules ]

Most Unix systems allow a declaration like
        int i;
to appear in more than one module.

Other systems require exactly one defining instance.  Technically,
        int i;
is a defining instance, while
        extern int i;
is not.

To be portable, the header file should contain
        extern int i;
and one module should contain
        int i;
(It is ok to have both
        extern int i;
        int i;
in the same module.)  This will compile and link correctly on all systems.

To avoid the problem of having to write declarations twice, here is a
trick that is often used:  Each header file looks like this:
        #ifndef EXTERN
        #define EXTERN extern
        #endif
        EXTERN int i;
        EXTERN double d;
        EXTERN struct foo bar;
        ...

Then when you include any header, all the variables are declared like this:
        extern int i;
        extern double d;
        extern struct foo bar;
In one module, such as the one including 'main()', you do this:
        #define EXTERN
        #include "file1.h"
        #include "file2.h"
        ...
Here 'EXTERN' is defined to be nothing, so the declarations now look
like this:
        int i;
        double d;
        struct foo bar;
You have a single source declaration for each variable, and exactly one
defining instance in the entire program for each.
--

Steve Clamage, TauMetric Corp, steve at taumet.com

Date: Wed, 22 May 91 8:01:36 PDT
From: Stephen D. Clamage <steve at taumet.com>
In-Reply-To: <9105220040.AA06573 at marlboro.nff.ncl.omron.co.jp>; from "Zhou Mi" a
t May 22, 91 9:40 am
X-Mailer: ELM [version 2.3 PL2]
Message-Id: <9105220801.AA17473 at taumet.com>
Status: RO

>       But why ---
> >> Most Unix systems allow a declaration like
> >>        int i;
> >> to appear in more than one module.
>               ??

This is called the "common" model, from the FORTRAN style of global
variables.  In FORTRAN, you didn't have global variables, but you
did have named sections of memory, called COMMON blocks.  Every module
(in original FORTRAN, every subroutine was its own module) which
referenced a COMMON block carried a declaration for it.  There was
no notion of a "defining instance" of a COMMON block, so the linker
had to take all these named blocks and allocate them to the same
address.

Early Unix C compilers used the same principle.  Each declaration of
a global data object was given the "common" attribute, and the
linker would allocate all global data items with the same name to the
same address.  This worked even if some were initialized.  Some linkers
would complain if more than one was initialized, and some would not;
in the latter case, you could not necessarily predict which initial
value would be used.

In some environments, the linker does not support common data, or
cannot reconcile an initialized item with a common item having the
same name.  So the most portable thing is not to use declarations
which result in objects with the "common" attribute.

--

Steve Clamage, TauMetric Corp, steve at taumet.com

3)
From: volpe at camelback.crd.ge.com (Christopher R Volpe)
Newsgroups: comp.lang.c
Subject: Re: Correct or Not or Old-fashioned or Bug
Date: 22 May 91 15:43:21 GMT
Reply-To: volpe at camelback.crd.ge.com (Christopher R Volpe)
Distribution: comp.lang.c

In article <1991May21.150735.12200 at convex.com>, grogers at convex.com
(Geoffrey Rogers) writes:
|>In non-ANSI C compiler/loader environments this may or may not work. You
|>could get multiple defined symbols for i, depending upon the model that the
|>compiler used for external variables.
|>
|>The above code in not ANSI conforming, because you do have multiple
|>definitions of i.

I think so far I have seen about 3 or 4 wrong answers to this question, such
as the above. There is nothing wrong with the referenced code as far as
the Standard is concerned. There are not multiple definitions of i, because
none of those *tentative* definitions has an initializer. In such a
situation, all tentative definitions are treated as a single definition
with initializer 0. (See A10.2 in K&R2 for an explanation)

-Chris

|>+------------------------------------+---------------------------------+
|>| Geoffrey C. Rogers                       | "Whose brain did you get?"      |
|>| grogers at convex.com                 | "Abbie Normal!"                 |
|>| {sun,uunet,uiucdcs}!convex!grogers |                                 |
|>+------------------------------------+---------------------------------+

==================
Chris Volpe
G.E. Corporate R&D
volpecr at crd.ge.com

**********************************************************************
	My brain is going to CORE DUMP because of lacking of memory.

	Thanks again for all the answers.

--
+------
 From:   Zhou  Mi

 ||=======||{}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~{}
 ||\    / ||{} Zhou Mi, RZE, Systems R&D LAB., OMRON Co.                    {}
 || \  /  ||{} E-mail:   zhoumi at nff.ncl.omron.co.jp                         {}
 ||  \/   ||{} TEL:  075-951-5111  x   3176                                 {}
 ||  /\   ||{}=============================================================={}
 || /     ||{} What is AI ? This is the very question that AI machine can   {}
 ||/      ||{} answer, but one can not !!                                   {}
 ||=======||{}______________________________________________________________{}



More information about the Comp.lang.c mailing list