Topic: array-dimension in initializer-clauses with subaggregates


Author: joerg.barfurth@attglobal.net (Joerg Barfurth)
Date: Wed, 21 Nov 2001 00:13:12 GMT
Raw View
Hi all,

I have a question regarding the interpretation of 8.5.1. Consider the
following code:=20

  struct A { char const * text; int num; };

  A array[] =3D { "A", 1, "B", 2, "C", 3 };

  int const c =3D sizeof array/sizeof(A);

What should be the value of c ?

Recently we discovered that g++ 3 yields c=3D=3D6. Looking closely at 8.5=
.1
I think that this is correct to the letter of the standard.

OTOH I find it highly surprising. It also causes sizeable amounts of
code to break, as the other compilers we are or have been using
(including gcc 2.9x) yield c=3D=3D3. I thus would expect that this result
was not intended.

The argument that c=3D=3D6 is correct is straightforward:

According to 8.5.1/4 the dimension of the array is defined as the number
of initializers[*] in the brace-enclosed initializer-clause. This number
abviously is 6.

The initialization is further interpreted as follows:

To initialize these elements of the array two of the initializers are
taken for each such subaggregate of type A (8.5.1/11).=20

This way the initializer list does not contain enough initializers for
all members of the aggregate, so the remaining elements are
default-initialized (8.5.1/7).=20

Thus, the array is initialized exactly the same, as if the declaration
had been:

  A array[] =3D { { "A", 1 }, { "B", 2 }, { "C", 3 },
                {  0,  0 }, {  0,  0 }, {  0,  0 } };

or simply=20

  A array[6] =3D { "A", 1, "B", 2, "C", 3 };

This is surprising, because intuitively one thinks that the array should
get a size that is just enough to use all initializers that are given
(and no extra default-initialized elements).

That intuitive behavior would only yield the first three of these
elements (as if from "{ { "A", 1 }, { "B", 2 }, { "C", 3 } }" ).

Now my questions: Which of these two interpretations was intended ?
Which one would be correct in the C language [+] ?

IOW: How should this be reported:=20
- as a C++ defect
- as a bug of g++ 3
- as a bug of 'most every other compiler

Regards, J=F6rg

[*] I do not meticulously distinguish "initializer" and
"initializer-clause" here (cf. core issue 163). My justification: The
standard doesn't do it properly either and even the proposed fix to
issue 163 only cleans this up for one paragraph.=20
BTW: Should there be a DR to extend #163 to all of 8.5.1 ?

[+] FWIW: Comeau online (http://www.comeaucomputing.com/tryitout) yields
c=3D=3D3 in all modes (C++98, C99 and C90).

--=20
J=F6rg Barfurth                         joerg.barfurth@attglobal.net
<<<<<<<<<<<<< using std::disclaimer;  <<<<<<<<<<<<<<<<<<<<<<<<<<<<
Software Developer                    http://util.openoffice.org
StarOffice Configuration      =3D Jetzt erh=E4ltlich:StarOffice 6 Beta

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]