2

The question is already discussed here [a link] Does new[] call default constructor in C++?

Being a beginner and a self-learner, will appreciate positive comments kindly pls don't generalize it as broad,narrow and etc

When we use new[] each element is initialized by the default constructor except when the type is a built-in type. Built-in types are left unitialized by default.

Here it is said that the objects are initialized by the default constructor what does it mean? Is it like every object has now key initialized to zero(by the default constructor) because that is true when I printed p[2].key?

How can i check if the default constructor initializes an array or not being an int array it can't access the member key in main?

Also (i) prints garbage value but (ii) prints 0;

class People
{
    public:
      int key;
    People()
    {
        key=0;
    }
};

int main()
{
   int count=5;
   People *p=new People[count];
   cout<< p[2].key;

   // int *arr=new int[count]; ...(i)
   //cout<<*(arr+2);

  //int *arr=new int[count](); ... (ii)
 //cout<<*(arr+2)<<endl;

 return 0;
}
Community
  • 1
  • 1
Stack
  • 235
  • 3
  • 15
  • 1
    You might want to see http://stackoverflow.com/questions/620137/do-the-parentheses-after-the-type-name-make-a-difference-with-new. The same applies to the array form. – chris Mar 29 '15 at 12:41
  • And, in general, the defintion of [default constructor](http://en.cppreference.com/w/cpp/language/default_constructor) – aruisdante Mar 29 '15 at 12:48

1 Answers1

5

The rules for the initialization of allocated arrays are a bit more complicated than the default constructor being called. What the standard actually says is this (5.3.4 [expr.new] paragraph 7, not exactly quoted):

  1. When using a new expression like new T[n] the values are default initialized.
  2. When using a new expression like new T[n]() the values are direct initialized. For the case of arrays only the value initialization of direct initialization applies.

Default initialization basically means that for classes with a default constructor the default constructor is called and for classes without a user-defined default constructor (i.e., when there is no default constructor at all or the default constructor is defaulted using = default) the members are default initialized. Default initialization of built-in types does nothing, i.e., a default initialized built-in type is left uninitialized and reading the corresponding object before it got initialized results in undefined behavior.

Value initialization means that for classes with a default constructor the default constructor is called and for classes without a default constructor the members are value initialized. Value initialization of built-in types results in zero initialization with zero initialization doing a suitable initialization for built-in types (zero for integral and floating point types, null for pointers, false for bool, etc.).

So in your three examples you get:

  1. With new People[count] you get count objects of type People initialized by calling the default constructor of People.
  2. With new int[count] you get count default initialized, i.e., uninitialized, ints and the expression *(arr + 2) (which is equivalent to arr[2]) yield undefined behavior.
  3. With new int[count]() you get count zero initialized ints, i.e., they are all zero.
dyp
  • 38,334
  • 13
  • 112
  • 177
Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • *"for classes without a default constructor the members are default initialized"* Which cases do you intend to cover by this addition? – dyp Mar 29 '15 at 13:53
  • @dyp: I fixed you second comment. With respect to your first comment: I mean structures without a user-defined constructor (i.e., either no constructor at all or a defaulted default constructor). I'll clarify. – Dietmar Kühl Mar 29 '15 at 14:48
  • I'm slightly worried since this simplification doesn't cover cases where the there is no default constructor, not even the implicitly declared one. You seem to distinguish between a *class with a user-defined default ctor* and a *class with a (n implicitly or explicitly) defaulted default ctor*. (I can see that this makes both cases default/value init symmetric, while they're not in the Standard). – dyp Mar 29 '15 at 16:35
  • @dyp: yes, the description glosses over cases which don't compile. The referenced section uses direct initialization rather than value initialization, probably because it conflates the non-array and the array case. I'll modify the description above to use value initialization. – Dietmar Kühl Mar 29 '15 at 17:21