Topic: Default initialization for array of built-in types
Author: jaj2276@lycos.com (JJ)
Date: Mon, 8 Dec 2003 23:20:15 +0000 (UTC) Raw View
If I have the following code:
--- [snip] ---
int* someArray = new int[10]();
--- [/snip] ---
Does the C++ standard guarantee that the 10 integers will be default
initialized to 0? If so, can someone point out to me where in the
standard it says this? I'm aware that calling int() will give me
default initialization for one int, but I'm not sure of array's of
ints.
I have antecdotal evidence of this because:
int* someArray = new int[10];
sometimes gives me ints with garbage values but int[10]() has never
given me garbage values.
Thanks,
Josh
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]
Author: deepblue57x@yahoo.co.nz (Graeme Prentice)
Date: Tue, 9 Dec 2003 01:58:49 +0000 (UTC) Raw View
On Mon, 8 Dec 2003 23:20:15 +0000 (UTC), jaj2276@lycos.com (JJ) wrote:
>If I have the following code:
>
>--- [snip] ---
>
>int* someArray = new int[10]();
>
>--- [/snip] ---
>
>Does the C++ standard guarantee that the 10 integers will be default
>initialized to 0? If so, can someone point out to me where in the
>standard it says this? I'm aware that calling int() will give me
>default initialization for one int, but I'm not sure of array's of
>ints.
>
>I have antecdotal evidence of this because:
>
>int* someArray = new int[10];
>
>sometimes gives me ints with garbage values but int[10]() has never
>given me garbage values.
Yes it's "guaranteed" by both the 1998 and 2003 standards but not all
compilers do it. A new initialiser is defined in 5.3.4 para 1 as
( expression-list opt )
where the expression-list can be empty. 5.3.4 para 15 defines that empty
parentheses produce default initialization which means zeroing for array
types.
int() or T() gives value initialization (formerly default
initialization) as you say, including for array types. This was
discussed in a recent thread on C++.moderated "Aggregate
initialization" approx 27 Nov.
TC1 and value initialization changed the meaning of the expression T()
when T is a non POD without constructors. Before TC1, for such a T, the
default constructor was called, which does nothing for POD members of T
and default constructs non POD members of T (read the definition of
default initialisation in 8.5 para 5 to see this). With TC1 and the
2003 std, for a non POD without constructors, T() produces value
initialisation which means that non static data members and base classes
of T are value initialised - this has the effect of zero initialising
all POD members of T.
---
[ 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.jamesd.demon.co.uk/csc/faq.html ]