Re: what does this mean ?

Tech-Archive recommends: Fix windows errors by optimizing your registry



On Fri, 8 Jul 2005 02:37:56 +1000, John Carson wrote:

> "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote in message
> news:dh8vu1zurpc3.bpeiwb74kp4l$.dlg@xxxxxxxxxx
>> On Thu, 7 Jul 2005 11:23:54 +0300, Alex Blekhman wrote:
>>
>>> Probably, you're confusing it with: new CFoo vs. new CFoo().
>>
>> Now, the question is, "What's the difference?"
>
> 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++? :)

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.

--
Doug Harrison
Microsoft MVP - Visual C++
.



Relevant Pages

  • Re: Class default initialiation
    ... >> introduction into the standard. ... It simply initialises each data member ... >> state) unless explicit initialisation is provided. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Class default initialiation
    ... If it made into the standard, am sure Francis Glassborow would have ... It simply initialises each data member ... > state) unless explicit initialisation is provided. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Initial values of File scoped and Block level variables
    ... The Standard requires that all static and file scope objects that are not ... The Standard makes no such demands for automatic objects, ... initialisation is honoured, and the rest of the object initialised as ... It's a programmer choice. ...
    (comp.lang.c)
  • Re: Iinitialisation via default constructor semantics
    ... > So what you are saying essentially, is that a SomeStruct temporary is ... That's how the standard specifies it. ... > provides an initialisation to 0 and that no temporary is created. ... the Standard say anything specific to POD for copy-initialisation. ...
    (comp.lang.cpp)
  • operator delete
    ... There are second form of allocation/deallocation functions ... in the C++ standard. ... But how to invoke operator "delete" in second form? ...
    (comp.lang.cpp)