This is the mail archive of the
gcc-prs@gcc.gnu.org
mailing list for the GCC project.
c++/7023: C++ structure initialization problem if too few initializers given
- From: Gary dot Jenkins at tellabs dot com
- To: gcc-gnats at gcc dot gnu dot org
- Date: 13 Jun 2002 19:30:36 -0000
- Subject: c++/7023: C++ structure initialization problem if too few initializers given
- Reply-to: Gary dot Jenkins at tellabs dot com
>Number: 7023
>Category: c++
>Synopsis: C++ structure initialization problem if too few initializers given
>Confidential: no
>Severity: non-critical
>Priority: low
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Thu Jun 13 12:36:06 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Gary Jenkins
>Release: egcs-2.91.60
>Organization:
>Environment:
SunOS sunbc6148 5.6 Generic_105181-23 sun4u sparc SUNW,Ultra-5_10
>Description:
Problem:
Structure initialization doesn't seem to work correctly if you get too
few initializers.
Expected Behavior:
If too few initializers are given, unspecified structure members
should be initialized with zero.
Example:
struct Foo
{
const int a;
const int b;
};
Foo f = { 0 }; // should initilize f.a and f.b to zero
Reference:
C Language
----------
"The ANSI C Programming Language (Second Edition)" , Copyright 1988 by AT&T
Brian W. Kerninghan, Dennis M. Ritchie,
Page 219, section A8.7 "Initialization":
"If there are fewer initializers in the list then members of the structure,
the trailing members are initialized with 0".
C++ Language
------------
This is alluded to but not stated as clearly in the C++ bible:
"The C++ Programming Language (Special Edition", Copyright 2000 by AT&T
Bjarne Stroustrup
Page 89, section 5.2.1 "Array Initializers":
"If the initializer supplies too few elements, 0 is assumed for the
remaining array elements".
Page 102, section 5.7 "Structures"
"The notation used for initializing arrays can also be used for initializing
variables of structure types".
>How-To-Repeat:
File main.cc:
struct Foo
{
const int a;
const int b;
};
int main()
{
Foo f = { 0 }; // error: uninitialized const member `b'
return 0;
}
Compile main.cc:
> g++ main.cc
main.cc: In function `int main()':
main.cc:9: uninitialized const member `b'
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="main.cc"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="main.cc"
c3RydWN0IEZvbwp7CiAgICBjb25zdCBpbnQgYTsKICAgIGNvbnN0IGludCBiOwp9OwoKaW50IG1h
aW4oKQp7CiAgICBGb28gZiA9IHsgMCB9OyAgICAgICAgICAgICAgLy8gZXJyb3I6IHVuaW5pdGlh
bGl6ZWQgY29uc3QgbWVtYmVyIGBiJwoKICAgIHJldHVybiAwOwp9Cg==