Bug 57152 - Declaration of array without size no longer compiles
Declaration of array without size no longer compiles
Status: CLOSED NOTABUG
Product: Red Hat Raw Hide
Classification: Retired
Component: gcc (Show other bugs)
1.0
i386 Linux
medium Severity high
: ---
: ---
Assigned To: Jakub Jelinek
:
Depends On:
Blocks:
  Show dependency treegraph
 
Reported: 2001-12-05 20:37 EST by Bill Crawford
Modified: 2008-05-01 11:38 EDT (History)
0 users

See Also:
Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2002-01-14 05:47:24 EST
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)

  None (edit)
Description Bill Crawford 2001-12-05 20:37:38 EST
Description of Problem:
Fails to compile what I believe is valid code.  A *declaration* for an
array without size specified is being treated like a trailing empty array
structure member declaration, i.e. treated as though it were size one.  A
later *definition* of the array with initialisers is treated as an error.

Code that fails to compile:
int const foo[];
int main (int ac, char **av) { int const *ip = foo; return 0; }
int const foo[] = { 3, 2, 1, 0 };

Version-Release number of selected component (if applicable):
gcc-3.1-0.10

How Reproducible:
Try to compile the above ...

Actual Results:
[bill@desktop tmp]$ c++ -o Bug Bug.C 
Bug.C:2: uninitialized const `foo'
Bug.C:2: array size missing in `foo'
Bug.C:4: redefinition of `const int foo[]'
Bug.C:2: `const int foo[1]' previously declared here
Bug.C:4: excess elements in aggregate initializer
[bill@desktop tmp]$

Expected Results:
[bill@desktop tmp]$ /usr/bin/i386-redhat-linux7-c++ -o Bug Bug.C 
[bill@desktop tmp]$

Additional Information:
Strictly guesswork territory here, but is this to do with allowing an empty
array to be declared as the last member of a structure (the variable-size
hack) ?

I know that compiling with a previous version proves nothing, but this does
work with 2.96 :o) and is valid according to the grammar in the back of
Stroustrup [3rd].
Comment 1 Bill Crawford 2002-01-13 12:19:09 EST
OK, it should have been the "gcc-c++" package up there ...

If this is actually violating the ANSI standard I'd like to know; it doesn't
look like it.  Basically that error should only be issued if *by the end of the
translation unit* the array type has not been completed.  Since the element type
(int) is complete, it should not be an error to index the array in main(); the
incomplete declaration should be equivalent to "extern const int foo[]" (which
is accepted).
Comment 2 Bill Crawford 2002-01-13 12:21:46 EST
I was too specific in my intitial report, by the way;  the problem exists with
or without the "const" qualifier on the array, but there is an extra error when
the const is present.
Comment 3 rodrigc 2002-01-13 22:13:52 EST
This bug is not valid.  If I compile with the Digital Unix C++ compiler,
I get:
=============================================================================
cxx: Error: Bug.C, line 1: const variable "foo" requires an initializer
int const foo[];
---------------^
cxx: Error: Bug.C, line 1: incomplete type is not allowed
int const foo[];
----------^
cxx: Error: Bug.C, line 2: declaration has already been defined by variable
          "foo" (declared at line 1)
int const foo[] = { 3, 2, 1, 0 };
----------^
cxx: Info: 3 errors detected in the compilation of "Bug.C".
[cjrodrig@is02 /home10/c/j/cjrodrig]% cxx -c Bug.C
cxx: Error: Bug.C, line 1: const variable "foo" requires an initializer
int const foo[];
---------------^
cxx: Error: Bug.C, line 1: incomplete type is not allowed
int const foo[];
----------^
cxx: Error: Bug.C, line 2: declaration has already been defined by variable
          "foo" (declared at line 1)
int const foo[] = { 3, 2, 1, 0 };
----------^
cxx: Info: 3 errors detected in the compilation of "Bug.C".
=============================================================================

If the code example is changed to a 2 line example, it works with
gcc and Digital Unix C++:
int const foo[] = { 3, 2, 1, 0 };
int main (int ac, char **av) { int const *ip = foo; return 0; }
Comment 4 Bill Crawford 2002-01-14 05:47:19 EST
OK *shrug*

I know it's valid C ... I couldn't find anything to explain why it should have
become illegal in C++.  Must have missed something.  If you happen to know where
in the standard this is standardized (I went through a lot of stuff that Google
turned up, and the only impression I came away with was that this ought to be
fine ...) I'd appreciate someone telling me.  One day I might get to read the
thing.
Comment 5 Jakub Jelinek 2002-01-15 17:48:01 EST
ISO C++ doesn't have tentative definitions (unlike C).
Comment 6 Bill Crawford 2002-01-15 19:09:27 EST
Oh well ... chalk one up to experience.  Most of the other things which have
caught me out have been obvious violations of something.  I couldn't find any
reference to this in Stroustrup ... anyway, now, like Coleridge's wedding guest,
I am a sadder and wiser man ;o)

Note You need to log in before you can comment on or make changes to this bug.