Re: what does this mean ?



"Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
news:jo00euo0aw6e.13puhb98z01aw.dlg@xxxxxxxxxx
On Fri, 8 Jul 2005 02:37:56 +1000, John Carson wrote:

By section 5.3.4/15 of the standard, the second form does value
initialisation, the details of which are described in section 8.5/5.

To illustrate, consider the following as compiled by VC++ 8.0 Beta 2:

struct Test
{
     int x, y;
};

int main()
{
     Test *ptr1 = new Test;
     cout << ptr1->x << ',' << ptr1->y << endl;

     Test *ptr2 = new Test();
     cout << ptr2->x << ',' << ptr2->y << endl;

     delete ptr1;
     delete ptr2;
     return 0;
}

x and y are uninitialised in the first case and zero initialised in
the second case. This is a change that occurred between the 1998
version of the standard and the 2003 version.

Don't you mean the 1998 and 2003 versions of VC++? :)

I am not sure how to interpret the :). The standard itself changed. Thus the 1998 section 8.5/5 says nothing about value initialisation. The 2003 version of section 8.5/5 reads as follows:


<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
A program that calls for default-initialization or value-initialization of an entity of reference type is ill-formed. If T is a cv-qualified type, the cv-unqualified version of T is used for these definitions of zero-initialization, default-initialization, and value-initialization.
</standard>


Also, the zero-initialization occurs in the Test() case only if Test
is a POD type, which means, to a fair approximation, a type you can
write in C. In particular, it would not be done were Test defined as:

struct Test
{
  int x;
  std::string s;
};

While there's no user-defined ctor, the non-static std::string member
makes it a non-POD type, and "new Test()" would leave x uninitialized.

On my reading, your statement is in accordance with the 1998 standard, which said that new Test() would be default initialized. It is also what VC++ 8.0 Beta 2 does.


Under the 2003 standard, new Test() gives value initialization. Given your declaration of struct Test, the applicable item is:

- 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;

Thus x is value initialized, which means, by the last item in the list, that it is zero-initialized.


-- John Carson

.



Relevant Pages

  • Re: Inheritence problem
    ... > try to debug I notice that once it leaves the Animal Constructor, ... > Protected variables lose thier initialization value. ... Cow class every thing you typed here should work just fine. ... int GetY(); ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Build-in types initialization
    ... > Thanks for your answer, but if it is true, how do I define my custom class ... > way to distinguish between these 2 types of initialization when writing ... Both are handled by default constructor. ... The default constructor for int, float, bool initializes them to 0 /false. ...
    (comp.lang.cpp)
  • Re: List container passed as a reference
    ... > which uses a default constructor, ... alternate cstor or copy cstor is irrelevent. ... It is initialization, not assignment. ... >> int main ...
    (alt.comp.lang.learn.c-cpp)
  • Re: List container passed as a reference
    ... >>Point has are a default and a copy constructor, ... > is a cstor with an assignment operator that initializes publicly accessible ... struct Point ... this is initialization, but it does not use constructors ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Message Builder vs. a Build Method?
    ... Builder class would encapsulate all the complex algorithms for making ... OTOH, as Daniel T. suggests, sometimes the initialization requires unique processing for initialization that is clearly intrinsic to the object itself. ... Constructors tend to be fragile and it is difficult to manage errors when they occur in a constructor scope, so it is usually a good idea to keep the processing in constructors as simple as possible. ... When the initialization of attribute data requires complex processing AND it seems like is intrinsic ot the object, that justifies having a separate initialization method that is invoked immediately after the constructor. ...
    (comp.object)