Are the new std :: vector <int> elements initialized to 0?
Let's say we use std::vector<int>
or std::vector<long>
. As the vector grows in size, will the newly allocated elements be initialized to default 0, or does the programmer need to explicitly 0?
New items are value-initialized:
[C++11: 23.3.6.3/9]:
void resize(size_type sz);
Effects: Ifsz <= size()
, equivalenterase(begin() + sz, end());
. Ifsize() < sz
, addssz - size()
value-initialized elements to the sequence.
For int
and long
this means 0
:
[C++11: 8.5/7]:
To initialize an object, typeT
means :
- If
T
is a class of a class (possibly cv-qualit) (section 9) with a user supplied constructor (12.1), then the default constructor for is calledT
(and the initialization is ill-formed if itT
does not have a default constructor available);- if it
T
is a (possibly cv-qualified) non-union class without a user-supplied constructor, then the object is initialized to zero and ifT
s implicitly declared a default constructor is non-trivial, that constructor is called.- If
T
is an array type, then each element is value-initialized;- , the object is initialized to zero.
An object initialized by value initialization is considered constructed and therefore obeys the provisions of this International Standard for "constructed" objects, objects "for which a constructor has completed," etc., even if no constructor is called to initialize the objects.
Note, however, that this does not mean "reserved" space at the end of the vector. This space does not contain any valid elements, zero-initialized or otherwise. This answer and the standard wording only speaks about the items you get when you execute, resize
without giving an explicit meaning for your new items.
In paragraph 23.3.6.3/9 of the C ++ 11 standard (about std::vector::resize()
):
void resize(size_type sz);
Effects: if sz <= size (), equivalent to erasing (begin () + sz, end ()) ;. If size () <sz, adds initialized elements to the sz - size () sequence .
In addition, according to clause 8.5 / 7 of the standard:
In value-initialize, an object of type T means:
- if T is a (possibly cv-qualified) class of type (section 9) with a user-supplied constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T does not have an available default Constructor);
- if T is a (possibly cv-qualified) non-union class without a user-supplied constructor, then the object is initialized to zero and if Ts's implicitly declared default constructor is non-trivial, that constructor is called.
- if T is an array type, then each element is initialized with a value;
- , otherwise the object is initialized to zero .
This means that in case the int
newly created elements are value-initialized 0
.
Yes, when std::vector
resizing (possibly by doing std::vector::resize
) any new elements will be value-initialized. For a type such as int
or long
, value initialization invokes null initialization, which, as the name suggests, sets the value to 0.