Re: new X vs new X()

From: Rob Williscroft (rtw_at_freenet.co.uk)
Date: 06/22/04


Date: 22 Jun 2004 16:07:03 GMT

Victor Bazarov wrote in news:UCXBc.1476$ri.119739@dfw-read.news.verio.net
in comp.lang.c++:

>> Nowadays its value-initialized, which is a kind-of recursive
>> default-intialization.
>>
>> In 8.5/5
>>
>> 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;
>>
>> 8.5/7
>>
>> An object whose initializer is an empty set of parentheses,
>> i.e., (), shall be value-initialized.
>
> Thanks, Rob. So, in the case of 'new X', if X is a POD it is still
> _uninitialised_. The only difference now is that it's not "default-
> initialised" for PODs but "value-initialised", right?
>
> You don't have to reply, I'm just repeating here what Andrew Koenig's
> reply stated (I hope).
>

Ok :), but I will add that value-initialization isn't just for POD's,
it applies to any "non-union class type" without used defined
constructors.

#include <iostream>
#include <ostream>
#include <iomanip>
#include <vector>

struct X
{
  std::vector< int > vi;
  int x;
};

struct Y
{
  std::vector< int > vi;
  X x;
};

struct Z
{
  std::vector< int > vi;
  Y y;
};

void messitup()
{
  int array[ 1000 ];
  for ( int i = 0; i < 1000; ++i )
  {
    array[ i ] = 0xCCCCCCC;
  }
}

bool is_vi()
{
  Z z = Z();
  return z.y.x.x == 0;
}

bool is_new_vi()
{
  Z *z = new Z();
  return z->y.x.x == 0;
}

int main()
{
  messitup();

  std::cout
    << "value-initialization: "
    << std::boolalpha << is_vi()
    << std::endl
  ;

  std::cout
    << "fake-value-initialization: "
    << std::boolalpha << is_new_vi()
    << std::endl
  ;
}

1 out of 4 of my compilers returned true, true, 1 returned false, true.
I'm a bit dissapointed gcc 3.4 doesn't support this.

Rob.

-- 
http://www.victim-prime.dsl.pipex.com/


Relevant Pages

  • Very weird compiler bug or normal ISO C++ behaviour?
    ... I went across what seems possibly a bug of the compiler (VisualC 2005, ... the wrong constructor will be called.. ... struct Two: One { ...
    (microsoft.public.vc.language)
  • "new" on value types
    ... Consider the following struct: ... TestStruct ts = new TestStruct; ... constructor throws an exception, "ts" will remain with its original value. ... static TestStruct newTestStruct(int a, int b, int c) ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Questions
    ... Would you want that to call the parameterless constructor 100 times? ... struct Derived: Base ... either disallow this, or only *actually* use a Base even though you've ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: why the leaking? please advise
    ... you declared a pointer to a map that maps strings to B ... > struct B; ... > int a; ... > Is my B's copy constructor correct? ...
    (microsoft.public.vc.language)
  • Re: Problem with linker
    ... but to have actually written a default constructor. ... such as overloading on int and pointer types. ... conversion of 0 to CString requires a user-defined conversion, ... acceleration operator *(distance d, time_squared t2); ...
    (microsoft.public.vc.mfc)