From "Working Draft C++, 2012-11-02"
3.6.2 Initialization of non-local variables [basic.start.init]
2 Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5)
before any other initialization takes place.
Variables with static storage are at least zero initialized.
3.7.3 Automatic storage duration [basic.stc.auto]
2 [ Note: These variables are initialized and destroyed as described in 6.7. — end note ]
6.7 says nothing about how automatic variables are initialized.
3.7.4 Dynamic storage duration [basic.stc.dynamic]
...
3.7.4.1 Allocation functions [basic.stc.dynamic.allocation]
... There are no constraints on the contents of the allocated storage on return from the
allocation function. The order, contiguity, and initial value of storage allocated by successive calls to an
allocation function are unspecified.
8.5 Initializers [dcl.init]
7 To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the
initialization is ill-formed if T has no accessible default constructor);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.
If you provide an explicit initializer, any variable will have a known value.
If you don't provide an explicit initializer for a POD type, it depends on the storage class. Static or thread variables will be zero initialized, whereas automatic or dynamically allocated variables are not.
If you have a compound type, the same rules apply. If you have don't have an explicit initializer, through a (default) constructor or otherwise, the initial value of fundamental types depends on the storage class.
Finally, memory allocated through malloc
will be uninitialized, whereas calloc
memory will be zero initialized.