[comp.std.c] question about linkage

mself@next.com (Matthew Self) (10/22/89)

Is K&R2 correct in saying (p. 227):

    If the first external declaration for a function
    or object includes the static specifier, the
    identifier has internal linkage; otherwise it has
    external linkage.

If so, then the identifier foo has external linkage in the
following program:

extern int foo[];

/* lots of references to foo here. */

static int foo[] = {0, 1, 2, 3};

How, then, can you forward declare an object with internal
linkage whose size isn't known when you forward declare it?
The logical thing to use appears to be:

static int foo[];

/* lots of references to foo here. */

static int foo[] = {0, 1, 2, 3};

Since the first declaration will be taken to be a tentative
definition of foo, which will later be demoted to a declaration
when the second definition is seen.

Unfortunately, every compiler I have tried complains that foo has
unknown size when you try this.

Also worth mentioning is the fact that traditional compilers seem
to give foo internal linkage rather than external in the first
example, indicating that this was how they solved the problem before
ANSI came along.  ANSI seems to have perverted the traditional
method for doing this without providing an alternative.

It is not possible to simply reorder the code to avoid the forward
reference, since this code is generated by a translator, and this
would require huge amounts of buffering!

Matthew Self
mself@next.com

henry@utzoo.uucp (Henry Spencer) (10/22/89)

In article <80@bomb.next.com> mself@bomb.UUCP (My Account) writes:
>How, then, can you forward declare an object with internal
>linkage whose size isn't known when you forward declare it?
>[old solution which sometimes works is non-ANSI, ANSI solution is not
> acceptable to some old compilers]

This is one of those annoying cases where you just have to break down
and say "#if __STDC__".  Many people, including me, would have preferred
that the linkage of a tentative definition be tentative, and subject
to revision when a real definition shows up.  This does, unfortunately,
cause real difficulties for one-pass compilers on machines with difficult
linkers.  The old behavior that you refer to wasn't dependably portable.
-- 
A bit of tolerance is worth a  |     Henry Spencer at U of Toronto Zoology
megabyte of flaming.           | uunet!attcan!utzoo!henry henry@zoo.toronto.edu

mself@next.com (Matthew Self) (10/24/89)

henry@utzoo.uucp (Henry Spencer) writes:
>In article <80@bomb.next.com> mself@bomb.UUCP (My Account) writes:
>>How, then, can you forward declare an object with internal
>>linkage whose size isn't known when you forward declare it?
>>[old solution which sometimes works is non-ANSI, ANSI solution is not
>> acceptable to some old compilers]
						   ^^^^^^^^^^^^^
What ANSI solution?

How can you forward declare a static object of variable size in ANSI C?

This doesn't work:

static int foo[];

/* many uses of foo here. */

static int foo[] = {0, 1, 2, 3};    /* now that I know how big it is */

GCC will not compile this, complaining that the first declaration of foo
has unknown size.  I asked RMS whether ANSI required this behaviour,
and he said yes.  Is this a bug in GCC?

As far as I have been able to tell, there is NO way to forward declare
a static array in ANSI C.  This is a serious step backward!