This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Partial aggregate initialization warning proposal


Using GCC 3.3.3 (and presumably later revisions), I compile following
test C++ program fragment (the definition is at global scope):

int PartialInitialized[ 3 ] = { 0, 1 };

I get no warnings or errors. GCC is behaving correctly since
(INCITS+ISO+IEC+14882-2003) 8.5.1-7 calls out for value-initialization
for the non-explicitly initialized members.

However, I often find that I am creating tables of data (I am an
embedded processor programmer by specialization). Sometimes these tables
get may get large and/or complicated and it is possible (especially
after program modifications and maintenance) that the number of
initializers does not match the number of members even though that was
intended/required. When there are too many initializers, the compiler
usefully catches the error. But when the are not enough, it provides no
help.

The specification allows for the general case of not enough explicit
initializes and defines what should happen. So I believe that the
compiler must not produce an error (and I believe it should not warn
either since often this is not desired).

But I'd still like some help :-)

I propose/request the following attribute (for both C and C++) to tell
the compiler that the number of initializers should exactly match the
number of members and when there are fewer it would output a warning:

explicit
     Use this attribute to specify that an aggregate object should be
completely explicitly initialized. If it is not, output a warning.
Examples:

    int Array[ 2 ] __attribute__((explicit)) = { 0 };  // warning: not
completely explicitly initialized 'Array'
    struct { int A;
             int B;
           } __attribute__((explicit)) Structure = { 3 }; // warning:
not completely explicitly initialized 'Structure'


I believe this would be a useful programmers' aid. Comments?


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]