Re: what does this mean ?
- From: "John Carson" <jcarson_n_o_sp_am_@xxxxxxxxxxxxxxx>
- Date: Fri, 8 Jul 2005 04:01:26 +1000
"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
.
- Follow-Ups:
- Re: what does this mean ?
- From: Doug Harrison [MVP]
- Re: what does this mean ?
- References:
- what does this mean ?
- From: Polaris
- Re: what does this mean ?
- From: Igor Tandetnik
- Re: what does this mean ?
- From: Lawrence Groves
- Re: what does this mean ?
- From: Alex Blekhman
- Re: what does this mean ?
- From: Doug Harrison [MVP]
- Re: what does this mean ?
- From: John Carson
- Re: what does this mean ?
- From: Doug Harrison [MVP]
- what does this mean ?
- Prev by Date: Re: what does this mean ?
- Next by Date: VS.Net-2005: Can't access a design resource form in one project
- Previous by thread: Re: what does this mean ?
- Next by thread: Re: what does this mean ?
- Index(es):
Relevant Pages
|