2

The question is in the topic. I'm also curious if the behavior differs for primitive type vs objects?

GingerPlusPlus
  • 5,336
  • 1
  • 29
  • 52
user3751232
  • 23
  • 1
  • 5
  • 3
    This should answer all your questions: http://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat – M.M Sep 07 '14 at 04:59

3 Answers3

2

This is a tricky question to answer accurately. It has been asked (and answered) many times, but I couldn't find an exact duplicate.

First, declaring an array does not allocate any storage and it does not cause any constructors to be executed. Perhaps you meant defining of allocating an array.

An array that is defined in static storage (outside any block) has its storage filled with zeros, no matter what type it is.

An array that is defined in automatic storage (inside a block) is allocated uninitialised memory.

An array that is created by the allocation of dynamic storage (using new[]) is allocated uninitialised memory.

Regardless of the method of storage allocation, the array is then default-initialised. What that means is defined in n3797 S8.5/7:

To default-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9), the default constructor (12.1) for T is called (and the initialization is ill-formed if T has no default constructor or overload resolution (13.3) results in an ambiguity or in a function that is deleted or inaccessible from the context of the initialization);
— if T is an array type, each element is default-initialized;
— otherwise, no initialization is performed.

In other words, arrays of class types have their default constructor called on each element and others are left as is.

See also /12:

If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value.

All this goes out the window if you add an initialisation to the array definition, but we'll leave that for another question.

david.pfx
  • 10,520
  • 3
  • 30
  • 63
0

Let us take an example.

struct Object
{
    Object(){
        std::cout << "new object is created";
    }
}
Object object1; // contructor is called here
Object object2; // and here

std::list<Object> my_list = {object1,object2}; //step 2

At step 2, you are not creating new objects of type Object, you are simply inserting them in my_list. Note: inserting in an array results in similar behavior.

Regarding your second question, primitive types are POD (i.e. Plain Old Data) types which by definition cannot have user-defined constructor.

Kam
  • 5,878
  • 10
  • 53
  • 97
0

Yes, arrays have all of their elements default-initialized (calling object's default constructors, or indeterminate for primitives (unless zero-initialization also takes place, in which case primitives are set to zero)).

However, some classes do not specify a default constructor. In these cases, you cannot have an array of them without initializing the array, since the array won't be able to default-initialize the objects. You would have to use a different collection, such as a vector.

jackarms
  • 1,343
  • 7
  • 14
  • 1
    Default-initializing for primitive types does not zero them. – M.M Sep 07 '14 at 04:27
  • @MattMcNabb Right, thanks for the distinction. It depends on the scope anyway. – jackarms Sep 07 '14 at 04:56
  • If the array is of static storage duration then it is *zero initialized* (i.e. primitives do get `0` or equivalent) , but people sometimes write "static" to mean "static or automatic" , so it's anyone's guess exactly what OP is asking. – M.M Sep 07 '14 at 04:58