This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 4 years ago.
class Example{
public:
int k;
};
int main(){
Example *ex = new Example();
Example *ex1 = new Example;
}
From what I have read so far, in the case of ex variable k will be value initialized, that means equals 0, and ex1 will be default initialized, and for basic types as int that means undefined behavior, but not 0. The problem is, when I print them
cout << ex->k << endl;
cout << ex1->k << endl;
it prints 0 for both. Why?
EDIT:
According to C++ Standard -
To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject
is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.
To default-initialize an object of type T means:
— if T is a non-POD 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, the object is zero-initialized.
To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized
The things look a little bit different to me now, in the case of ex1 we have default initialization, it's a POD class and not an array so , the object is zero-initialized, then - if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized; and that means that k will be initialized to 0 and it is not undefined behavior. Right ?
"Undefined behavior" means the compiler can do anything .Which is to say, that when you hit undefined behavior, the compiler would be completely within its rights to make demons fly out of your nose nasal demons.Thankfully,in your case it chose to print 0.
Related
Consider following program:
#include <iostream>
struct Test
{
int a;
};
int main()
{
Test t=Test();
std::cout<<t.a<<'\n';
}
Test t=Test(); value initializes a temporary & copy initializes it. (most compilers optimize out the copy operation (Source: value initialization)). But value initialization is introduced by C++03. What happens in C++98 when Test t=Test(); is executed? Is it guaranteed that I will get 0 as an output (value of t.a in this case) on any C++98 compiler? . Is it default initialization being performed in C++98?
C++ standard (1998)
[dcl.fct.def]
7 An object whose initializer is an empty set of parentheses, i.e., (), shall be default-initialized.
[dcl.init]
5 To zero-initialize storage for an object of type T means:
— if T is a scalar type (3.9), the storage is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, the storage for each nonstatic data member and each base-class subobject is zero-initialized;
— if T is a union type, the storage for its first data member 89) is zero-initialized;
— if T is an array type, the storage for each element is zero-initialized;
— if T is a reference type, no initialization is performed.
To default-initialize an object of type T means:
— if T is a non-POD 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, the storage for the object is zero-initialized.
It appears that the temporary is default initialized, which means zero initialization for a POD type (which Test is) and therefore t.a == 0 is guaranteed.
Since C++03 this is value initialization and same guarantee remains.
It appears that the addition of value initialization and re-definition of default initialization in C++03, was to allow not zero-initializing scalars and POD types (in some contexts).
Does int a = int(); necessarily give me a zero?
How about if int is replaced by char, double, bool or pointer type?
Where is this specified in the language standard, please?
Does int a = int(); necessarily give me a zero?
Yes, the standard guarantees that it gives you zero.
This is known as Value Initialization. For the type int, Value Initialization basically ends up being an Zero Initialization.
Where is this specified in the language standard, please?
The rules are clearly specified in the standard in section 8.5.
I will quote the relevant ones to the Q here:
C++03: 8.5 Initializers
Para 7:
An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.
Value Initialization & Zero Initialization are defined in 8.5 Para 5 as:
To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default
constructor for T is called (and the initialization is ill-formed if T has no accessible
default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static
data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized
To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject
is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.
Note: The bold texts are emphasized by me.
Yes, any built-in type is always initialized to zero when default-initialized. Keep in mind that in most scenarios a built-in type is not default initialized so this won't necessarily print out 0:
int i;
std::cout << i << "\n";
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do the following phrases mean in C++: zero-, default- and value-initialization?
If I have a class for example:
class Info
{
int x;
int y;
};
which I used to created an object,
Info *p = new Info();
Does the brackets beside Info mean i'm value initializing it? How does it different from this, Info *p = new Info; ?
I know there is a question which differentiate between different meanings in new and old C++ language but I want to know the semantic difference between default and value initialization e.g. Does value initialization means initializing something to zero?
A declared variable can be Zero Initialized, Value Initialized or Default Initialized.
In your example:
Info *p = new Info(); <------- Value Initialization
Info *p = new Info; <------- Default Initialization
The C++03 Standard 8.5/5 aptly defines each:
To zero-initialize an object of type T means:
— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;
— if T is a non-union class type, each nonstatic data member and each base-class subobject
is zero-initialized;
— if T is a union type, the object’s first named data member is zero-initialized;
— if T is an array type, each element is zero-initialized;
— if T is a reference type, no initialization is performed.
To default-initialize an object of type T means:
— if T is a non-POD 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, the object is zero-initialized.
To value-initialize an object of type T means:
— if T is a class type (clause 9) with a user-declared constructor (12.1), then the default
constructor for T is called (and the initialization is ill-formed if T has no accessible
default constructor);
— if T is a non-union class type without a user-declared constructor, then every non-static
data member and base-class component of T is value-initialized;
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized
The C++ standard says (8.5/5):
To default-initialize an object of type T means:
If T is a non-POD 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, the object is zero-initialized.
With this code
struct Int { int i; };
int main()
{
Int a;
}
the object a is default-initialized, but clearly a.i is not necessarily equal to 0 . Doesn't that contradict the standard, as Int is POD and is not an array ?
Edit Changed from class to struct so that Int is a POD.
From 8.5.9 of the 2003 standard:
If no initializer is specified for an object, and the object is of
(possibly cv-qualified) non-POD class type (or array thereof), the
object shall be default-initialized; if the object is of
const-qualified type, the underlying class type shall have a
user-declared default constructor. Otherwise, if no initializer is
specified for a nonstatic object, the object and its subobjects, if
any, have an indeterminate initial value); if the object or any of
its subobjects are of const-qualified type, the program is ill-formed.
The class you show is a POD, so the highlighted part applies, and your object will not be initialized at all (so section 8.5/5, which you quote, does not apply at all).
Edit: As per your comment, here the quote from section 8.5/5 of the final working draft of the current standard (I don't have the real standard, but the FDIS is supposedly very close):
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.
Your variable is not initialized.
Use
Int a = Int();
to initialize your POD or declare a standard constructor to make it non POD;
But you can also use your POD uninitialized for performance reasons like:
Int a;
a.i = 5;
No, the object a is not default-initialized. If you want to default-initialize it, you have to say:
Int a = Int() ;
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do the following phrases mean in C++: zero-, default- and value-initialization?
I am confused about an issue in C++.
When creating an object on the stack using the default constructor, I thought either one of the following two syntax notations would give the same result:
class MyClass { public: int i; }
int main()
{
MyClass a = MyClass();
MyClass b;
}
However, the first syntax initializes the field to zero, whereas the second one leaves the field uninitialized. So my questions are:
Why is this so? I thought fields in C++ were not supposed to be automatically initialized.
Is there any other differences between the two syntaxes?
Do these syntax variations have separate names to distinguish them from each other?
I’m currently using Microsoft Visual C++ 2010 Express.
Thanks!
In the first, you copy-initialize a from a value-initialized instance of MyClass. From the C++03 standard, §8.5/7:
An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.
And from §8.5/5:
To value-initialize an object of type T means:
if T is a class type with a user-declared constructor, then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
if T is a non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
if T is an array type, then each element is value-initialized;
otherwise, the object is zero-initialized
To zero-initialize an object of type T means:
if T is a scalar type, the object is set to the value of 0 (zero) converted to T;
if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
if T is a union type, the object’s first named data member) is zero-initialized;
if T is an array type, each element is zero-initialized;
if T is a reference type, no initialization is performed.
In the second, you declare b in a manner that would cause it to be default-initialized if MyClass were not a POD type -- §8.5/5:
To default-initialize an object of type T means:
if T is a non-POD class type, 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, the object is zero-initialized.
However, because MyClass is a POD type, b is uninitialized -- §8.5/9:
If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a non-static object, the object and its subobjects, if any, have an indeterminate initial value; if the object or any of its subobjects are of const-qualified type, the program is ill-formed.
Basically, this is a (relatively) simple WTF in the language, where primitive types will not be initialized by default. The first syntax explicitly initializes them- the second doesn't. User-defined types will always be initialized, so it's only meaningful if you don't initialize it in the constructor, and it will be an error if you don't call the init functions of UDTs that need them.
UDTs that do not do anything insane should not require the first syntax and it's normal to use the second.