[comp.std.c] Why can't you define an external object twice and never use it?

eggert@rise.twinsun.com (Paul Eggert) (11/10/89)

The program

	int X = 0;
	int X = 0;
	main() { return 0; }

is ``obviously'' not ANSI C, but where does the proposed ANSI standard say so?
X has multiple external definitions with external linkage, but X is never used.

3.1.2.2 prohibits the same identifier from appearing with both internal and
external linkage.  3.5's constraints prohibit multiple declarations of
identifiers with no linkage, and also prohibit declarations in the same scope
that refer to the same object but specify incompatible types.  3.7's
constraints prohibit multiple external definitions with internal linkage.
3.7's semantics prohibit multiple external definitions of any identifier that
is declared with external linkage and used in an expression.

However, none of these prohibitions apply to the above program.  What's wrong?

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/11/89)

In article <53@looney.twinsun.com> eggert@twinsun.com (Paul Eggert) writes:
>	int X = 0;
>	int X = 0;
>However, none of these prohibitions apply to the above program.  What's wrong?

I don't know why you cite constraints on multiple definition;
the real problem is that you have violated the "only one initialization"
constraint.

walter@hpclwjm.HP.COM (Walter Murray) (11/14/89)

Paul Eggert writes:

> The program
> 	int X = 0;
> 	int X = 0;
> 	main() { return 0; }
>is ``obviously'' not ANSI C, but where does the proposed ANSI standard say so?
>X has multiple external definitions with external linkage, but X is never used.

I think it's even worse.  As far as I can tell, even the following 
program is strictly conforming, but I'll be happy if someone can
prove otherwise.

      int object = 1;
      int object = 2;
      function () { return 3; }
      function () { return 4; }
      main () { }

Looks like an oversight in the dpANS.

Walter Murray
---

eggert@twinsun.com (Paul Eggert) (11/14/89)

I asked what was wrong with the external definitions `int X = 0; int X = 0;'
if X is used nowhere else.  Doug Gwyn replied:

	I don't know why you cite constraints on multiple definition;
	the real problem is that you have violated the "only one
	initialization" constraint.

Where is this constraint written down?  It's not in either pANS 3.5.7
("Initialization") or pANS 3.7 ("External definitions"), where I would
have expected it.  I fear it was so obvious that it wasn't written
down anywhere.

gwyn@smoke.BRL.MIL (Doug Gwyn) (11/14/89)

In article <54@looney.twinsun.com> eggert@twinsun.com (Paul Eggert) writes:
->the real problem is that you have violated the "only one
->initialization" constraint.
-Where is this constraint written down?  It's not in either pANS 3.5.7
-("Initialization") or pANS 3.7 ("External definitions"), where I would
-have expected it.  I fear it was so obvious that it wasn't written
-down anywhere.

It's there somewhere; I remember seeing it a few days ago while
looking for something else.  Maybe it's part of the definition
of an object.