Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Initializers

Reply
Thread Tools

Initializers

 
 
Michael Tsang
Guest
Posts: n/a
 
      04-19-2010
I'm very confused among different types of initializations:

zero-initialize:
scalar: set to 0
class/array: initialize recursively
union: initialize first member
reference: no-op

default-initialize:
class: call default constructor
array: initialize recursively
reference: ill-formed
others: no-op

value-initialize:
class with user-provided constructor: call default constructor
non-union class without user-provided constructor: zero-
initialise and call non-trivial constructor
array: initialize recursively
reference: ill-formed
others: zero-initialize

implicitly-defined default constructor:
default-initialize base classes and members recursively

When the direct initializer is (), the object is value-initialized but
consider the following:

#include <iostream>
struct A
{
A()
{
std::cout << "test" << std::endl;
}
int x;
};

struct B
{
// non-trivial implicit default constructor
A a;
int x;
};

A x; // x is zero-initialized and then default initialized so that x.x
== 0

int main()
{
A *p = new A; // p->x is not determined.
A *q = new A(); // q->x is also not determined?! A
standard caveat?!
delete p;
delete q;
B *r = new B; // default-initialized recursively: s->x
and s->a.x are both indeterminate.
B *s = new B(); // zero-initialized first: s->x and s->a.x
are both 0
}

Also, how does zero-initializing a reference make sense?

 
Reply With Quote
 
 
 
 
Michael Tsang
Guest
Posts: n/a
 
      04-20-2010
Victor Bazarov wrote:

> Michael Tsang wrote:
>> I'm very confused among different types of initializations:
>>
>> zero-initialize:
>> scalar: set to 0
>> class/array: initialize recursively
>> union: initialize first member
>> reference: no-op

>
> Huh? Where do you get the "no-op"?
>
>>
>> default-initialize:
>> class: call default constructor
>> array: initialize recursively
>> reference: ill-formed
>> others: no-op
>>
>> value-initialize:
>> class with user-provided constructor: call default constructor
>> non-union class without user-provided constructor: zero-
>> initialise and call non-trivial constructor
>> array: initialize recursively
>> reference: ill-formed
>> others: zero-initialize
>>
>> implicitly-defined default constructor:
>> default-initialize base classes and members recursively
>>
>> When the direct initializer is (), the object is value-initialized but
>> consider the following:
>>
>> #include <iostream>
>> struct A
>> {
>> A()
>> {
>> std::cout << "test" << std::endl;
>> }
>> int x;
>> };
>>
>> struct B
>> {
>> // non-trivial implicit default constructor
>> A a;
>> int x;
>> };
>>
>> A x; // x is zero-initialized and then default initialized so that x.x
>> == 0
>>
>> int main()
>> {
>> A *p = new A; // p->x is not determined.
>> A *q = new A(); // q->x is also not determined?! A
>> standard caveat?!

>
> Why is that a caveat? If you provide the default constructor and
> *purposefully* omit 'x' from the initializer list, it's left
> uninitialized no matter whether you use parens here or not.
>
>> delete p;
>> delete q;
>> B *r = new B; // default-initialized recursively: s->x
>> and s->a.x are both indeterminate.
>> B *s = new B(); // zero-initialized first: s->x and s->a.x
>> are both 0

>
> Zero-initialized first? Where do you get that? It's not static.

I got it from the latest draft (N3092).
>
> The dynamic B object is value-initialized, yes (see 5.3.4/15)? And
> since 'B' is an aggregate (no user-defined c-tors, no private or
> protected non-static members, no virtual functions, no base classes), to
> value-initialize it means that every non-static member and every base
> class is value-initialized. And since 'A' has a user-defined c-tor, to
> value-initialize it means to call that c-tor.
>
> So, nope. Since 'A' has a user-declared c-tor, s->a.x is still
> uninitialized!
>
>> }
>>
>> Also, how does zero-initializing a reference make sense?

>
> There ain't no such a thing as "zero-initializing a reference".

The document does mention zero-initializing a reference.
>
> V


 
Reply With Quote
 
 
 
 
tonydee
Guest
Posts: n/a
 
      04-20-2010
On Apr 20, 11:41*am, Michael Tsang <(E-Mail Removed)> wrote:
> Victor Bazarov wrote:
> > Michael Tsang wrote:
> >> I'm very confused among different types of initializations:


Don't make it too complex. The important thing is just that your
constructor should explicitly populate the values to get the class
into a good state - unless they have adequate default constructors
themselves (e.g. STL containers) - and assume anything else will be
indeterminate. That's ok if the indeterminate value(s) will
definitely be written to before being read.

Cheers,
Tony
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      04-20-2010
* Michael Tsang:
> I'm very confused among different types of initializations:


[snip]

All that you need to remember and that you can rely on is

* Static duration objects are zero initialized before anything else.

* Apart from that, you get the initialization that you specify.

E.g. if you have defined at least one constructor for your class, then a
constructor will be called for every instance of that class (except if one does
nasty low-level things).

And e.g., when you write

T* const p = new T();

then the parenthesis says that you want initialization, and for each member of T
you get the initialization that is available (zeroing or a constructor call).

If you omit the parenthesis then you say that you don't care about
initialization and that you will be happy with whatever initialization that T
defines, if any.


> }
>
> Also, how does zero-initializing a reference make sense?


The standard defines it thusly, in 8.5/5: "no initialization is performed".

It's that simple.

Of course since you can't use the reference without UB before it's been properly
initialized, a compiler may choose to represent a namespace level reference as a
pointer and zero the pointer value so that &r, if it didn't do any of all the
arbitrary things that the UB allows, would yield a null-pointer at this time.


Cheers & hth.,

- Alf
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
exceptions in static initializers HK Java 3 05-31-2005 11:50 AM
static initializers vs. cached values HK Java 7 05-30-2005 05:28 PM
JNI Invocation: static UN-initializers? James Robert Leek Java 2 08-31-2004 09:13 PM
problem with static initializers j l Java 5 02-23-2004 09:58 PM
Class variable initializers Glen Able Java 0 01-29-2004 02:55 PM



Advertisments