Bug 57152 - Declaration of array without size no longer compiles
Summary: Declaration of array without size no longer compiles
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Red Hat Raw Hide
Classification: Retired
Component: gcc
Version: 1.0
Hardware: i386
OS: Linux
medium
high
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2001-12-06 01:37 UTC by Bill Crawford
Modified: 2008-05-01 15:38 UTC (History)
0 users

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2002-01-14 10:47:24 UTC
Embargoed:


Attachments (Terms of Use)

Description Bill Crawford 2001-12-06 01:37:38 UTC
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 17:19:09 UTC
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 17:21:46 UTC
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-14 03:13:52 UTC
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 10:47:19 UTC
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 22:48:01 UTC
ISO C++ doesn't have tentative definitions (unlike C).

Comment 6 Bill Crawford 2002-01-16 00:09:27 UTC
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.