Reputation: 6871
In C++ Primer 5th (chapter 12),
By default, dynamically allocated objects are default initialized, which means that objects of built-in or compound type have undefined value;
int *pi = new int; // unitialized int
This statement indicates that built-in type object has undefined value when default initialized. However, the behaviour of default initialized built-in type object depends on where it is defined.
To be specific, built-in type object outside any function shall be 0
, while built-in type object inside some block has undefined value.
Hence, I think the statement above is not accurate,since for built-in type:
default initialized != undefined value
Do I understand this properly?
Upvotes: 3
Views: 168
Reputation: 119134
For non-class types, default initialization performs no initialization.
However, variables with static or thread storage duration are always zero-initialized before any other initialization takes place. So if you have int x;
at the global scope, although the default initialization does nothing, x
is still initialized to zero due to the zero-initialization that takes place before the default initialization.
For a non-class object with dynamic storage duration, if no initializer is given, the value is indeterminate because zero-initialization does not apply.
Upvotes: 8