Object Initialisation

F

Frederick Gotham

I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}


And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};


int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}



The Standard tells me that there are three kinds of initialisation:


Zero-initialisation
Default-initialisation
Value-initialisation


In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation", as I'm giving the object its default
value.

What is zero-initialisation, and can anyone give an example of it please?

What is value-initialisation, and can anyone give an example of it please?


Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};
 
V

Victor Bazarov

Frederick said:
I have just been reading 8.5 in the Standard, and am trying to make
sense of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised
(i.e. containing garbage), or being default initialised (i.e.
containing the default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}


And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};


int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}



The Standard tells me that there are three kinds of initialisation:


Zero-initialisation
Default-initialisation
Value-initialisation

And there is the fourth kind: uninitialised.
In my above code snippet, what kind of initialisation am I peforming?

In the first part, none. Objects are _uninitialised_. In the second
part the _default_.
I think it's "default initialisation", as I'm giving the object its
default value.

Right. Only in the second case.
What is zero-initialisation, and can anyone give an example of it
please?

That's the incarnation of the default-initialisation for PODs.
What is value-initialisation, and can anyone give an example of it
please?

int a(5);
Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};

Default.

V
 
F

Frederick Gotham

Victor Bazarov posted:

That's the incarnation of the default-initialisation for PODs.


The reason we invent words is not so we have a label for something, but so
that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same? If so, why give them different names?

Can you please give an example of zero-initialising an object?

In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */


MyStruct static obj = {}; /* zero or default? */


MyStruct obj = {}; /* zero or default? */
}
 
V

Victor Bazarov

Frederick said:
Victor Bazarov posted:




The reason we invent words is not so we have a label for something,
but so that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same?

No. Yes. For PODs they are the same. For non-PODs they are not
the same.
If so, why give them different names?

Because they are not, in general case.
Can you please give an example of zero-initialising an object?

static int a;
void *p(0);
In the following code, what kind of initialisation takes place?

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
MyStruct static obj; /* zero or default? */
Zero.

MyStruct static obj = {}; /* zero or default? */

First zero, then default (which in this case superfluous).
MyStruct obj = {}; /* zero or default? */

Default, which for POD is zero-.

V
 
F

Frederick Gotham

Victor Bazarov posted:
No. Yes. For PODs they are the same. For non-PODs they are not
the same.


Does zero-initialise mean that you explicitly write the numeral figure
zero, as in:

int i = 0;

double b = 0;

int *p = 0;

?


All the above initialisations are equivalent to:


int i = int();

double b = double();

typedef int *intptr;
int *p = intptr();


I still don't understand how zero-initialisation is any different to
default-initialisation. . .
 
V

Victor Bazarov

Frederick said:
Victor Bazarov posted:



Does zero-initialise mean that you explicitly write the numeral figure
zero, as in:

int i = 0;

double b = 0;

int *p = 0;

?

Sort of. '0' is an octal int literal, intialising a 'double' with it
potentially involves a temporary of type 'double' and initialising
a pointer with it potentially involves a temporary null pointer. The
compiler probably does everything behind the scenes and during compile-
time, anyway.
All the above initialisations are equivalent to:


int i = int();

double b = double();

typedef int *intptr;
int *p = intptr();
Right.

I still don't understand how zero-initialisation is any different to
default-initialisation. . .

For non-POD it involves calling the default c-tor. There are no c-tors
for PODs, and that's why there is no difference between 'default-' and
'zero-' for PODs. Just read the Standard.

V
 
S

Sabiyur

Frederick said:
I have just been reading 8.5 in the Standard, and am trying to make sense
of the different kinds of initialisations.

Up until now, I thought of an object as either NOT being initialised (i.e.
containing garbage), or being default initialised (i.e. containing the
default value for that type).

Here are some examples of the former:

struct MyStruct {

int a;
double b;
void *p;
};

int main()
{
int a; /* Contains garbage */

double b; /* Contains garbage */

void *p; /* Contains garbage */

MyStruct obj; /* All members contain garbage */
}


And here are some examples of the latter:

struct MyStruct {

int a;
double b;
void *p;
};


int main()
{
int a = int(); /* Contains 0 */

double b = double(); /* Contains 0.0 */

typedef void *voidptr;
void *p = voidptr(); /* Contains null pointer value */

MyStruct obj = {}; /* Each member has the same values as above */
}



The Standard tells me that there are three kinds of initialisation:


Zero-initialisation
Default-initialisation
Value-initialisation


In my above code snippet, what kind of initialisation am I peforming? I
think it's "default initialisation", as I'm giving the object its default
value.

What is zero-initialisation, and can anyone give an example of it please?

What is value-initialisation, and can anyone give an example of it please?


Also, what kind of initialisation does the following class perform?

template<class T>
struct Init {

T obj;

Init() : obj() {}
};

Hello Frederick,
I tried your code in VC++ 6.0. It throws compilation
errors.

All the errors point to the code MyStruct obj = {};

\Initialization.cpp(49) : error C2059: syntax error : '}'
\Initialization.cpp(49) : warning C4508: 'main' : function should
return a value; 'void' return type assumed
\Initialization.cpp(53) : error C2143: syntax error : missing ';'
before '}'
\Initialization.cpp(53) : error C2143: syntax error : missing ';'
before '}'
\Initialization.cpp(53) : error C2143: syntax error : missing ';'
before '}'

What compiler are you using?

All,
Is it correct way to initialize the struct like this?

Thanks
Sabiyur
 
V

Victor Bazarov

Sabiyur said:
Frederick said:
[..]
Frederick Gotham

Hello Frederick,
I tried your code in VC++ 6.0. [..]

Don't. Throw it away. It's so pre-standard, even it's children are
not compliant.
What compiler are you using?

All,
Is it correct way to initialize the struct like this?

Of course. Get a decent compiler. VC++ 2005 EE is available for
free from MS. No comparison, so much better standard compliance.

V
 
F

Frederick Gotham

Victor Bazarov posted:
For non-POD it involves calling the default c-tor. There are no c-tors
for PODs, and that's why there is no difference between 'default-' and
'zero-' for PODs. Just read the Standard.


I've read the Standard an I don't understand it.

Here's value initialisation:

MyStruct obj = { 1, 5.2, 0 };

Here's default initialisation:

MyStruct obj = {};


So what's zero initialisation?
 
V

Victor Bazarov

Frederick said:
Victor Bazarov posted:



I've read the Standard an I don't understand it.

Here's value initialisation:

MyStruct obj = { 1, 5.2, 0 };

Here's default initialisation:

MyStruct obj = {};


So what's zero initialisation?

Zero-initialisation is what happens with the POD when you
default-initialise it. So, the last statement is zero-initialisation
(*by definition* of the default-initialisation for PODs). Which part
of it do you not understand?

V
 
R

Robbie Hatley

Victor Bazarov said:
Don't. Throw it away. It's so pre-standard, even it's children are
not compliant.... VC++ 2005 EE is available for free from MS.
No comparison, so much better standard compliance.

I'm using VC++ 6.0 at work. I agree, it's not very compliant. I've
run into several cases where it does unexpected stuff. So I'd like
to upgrade.

HOWEVER.....

When I went to www.microsoft.com and check the pricing, the upgrade
kit from VC++ 6.0 to VC++ 2005 is not listed as free; it's listed
as $550.00. That's if you *qualify* for the upgrade. Otherwise,
the price is $800.

The company I work for is unlikely to agree to spend that kind of
money. (Yah, I know, it's small compared to the savings from
improved productivity; but that's how it is.)

Where did you see the "free" pricing for this product? They have
an "evaluation" version, but that's probably skeletal or
30-day-limited or splash-screen-corrupted. I've got one of
their freebie versions at home, and every program it compiles
displays a big ugly splash screen at startup that says something
about how "this program was compiled with an unlicensed copy
of Visual Studio and is not authorized for public distribution"
or words to that effect. That's useless for anything except as
a toy or teaching aid; can't use it at work.


--
Curious,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
 
N

Noah Roberts

Robbie said:
Where did you see the "free" pricing for this product? They have
an "evaluation" version, but that's probably skeletal or
30-day-limited or splash-screen-corrupted. I've got one of
their freebie versions at home, and every program it compiles
displays a big ugly splash screen at startup that says something
about how "this program was compiled with an unlicensed copy
of Visual Studio and is not authorized for public distribution"
or words to that effect. That's useless for anything except as
a toy or teaching aid; can't use it at work.

Express Edition is free and you can use it for anything I believe. It
doesn't seem to do solutions, has no source control connection,...yeah,
it's missing a few features. It does compile projects though and afaik
it doesn't add a bunch of crap to your exe to flag its use.

If your co. wanted to upgrade they should have gone to the launch. We
all got standard edition and a bunch of other useless crap for sitting
through an 8 hour commercial. At least I was getting paid ;)
 
M

Mark P

Frederick said:
Victor Bazarov posted:




The reason we invent words is not so we have a label for something, but so
that we can distinguish it from other things.

That is why I'm confused with "zero-initialsation" Vs. "default-
initialisation".

Are they not exactly the same? If so, why give them different names?

Compare the definitions in the standard which are one above the other.
For a non-POD class type, to default initialize is to call the default
constructor. To zero-initialize the same is to recursively
zero-initialize its base-class subobjects and nonstatic members which
eventually terminates with, essentially, everything being set to 0
(converted as appropriate). Naturally a default ctor could do something
other than set everything to zero, hence the two are not the same.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top