From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26855 invoked by alias); 13 Jun 2002 19:36:09 -0000 Mailing-List: contact gcc-prs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-prs-owner@gcc.gnu.org Received: (qmail 26784 invoked by uid 71); 13 Jun 2002 19:36:07 -0000 Resent-Date: 13 Jun 2002 19:36:07 -0000 Resent-Message-ID: <20020613193607.26783.qmail@sources.redhat.com> Resent-From: gcc-gnats@gcc.gnu.org (GNATS Filer) Resent-To: nobody@gcc.gnu.org Resent-Cc: gcc-prs@gcc.gnu.org, gcc-bugs@gcc.gnu.org Resent-Reply-To: gcc-gnats@gcc.gnu.org, Gary.Jenkins@tellabs.com Received: (qmail 23471 invoked by uid 61); 13 Jun 2002 19:30:36 -0000 Message-Id: <20020613193036.23467.qmail@sources.redhat.com> Date: Thu, 13 Jun 2002 12:36:00 -0000 From: Gary.Jenkins@tellabs.com Reply-To: Gary.Jenkins@tellabs.com To: gcc-gnats@gcc.gnu.org X-Send-Pr-Version: gnatsweb-2.9.3 (1.1.1.1.2.31) Subject: c++/7023: C++ structure initialization problem if too few initializers given X-SW-Source: 2002-06/txt/msg00313.txt.bz2 List-Id: >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==