1

There are many questions on this topic but many of them use inaccurate terms and the answers are mostly partial. I will try to ask it in yet a another way and hopefully come up with a polished answer to this frequent question.

  1. What is the difference, if any, between default-initialization and zero-initialization in the case of PODs?

  2. If a member POD variable is not initialized in the constructor initialization list, in which case will it be:

    • zero-initialized.
    • default-initialized (if different from above).
    • left uninitialized.
  3. Does the answer to question 2 above depend on whether the instance of the class is created on the stack or on the heap using the new operator

  4. Is the answer to any of the above questions different for C++98,C++11 or C++14?

Mustafa
  • 1,814
  • 3
  • 17
  • 25

1 Answers1

5
  1. In default initialization, basic "C"-style types (int, double, char, bool, etc.) have indeterminate values. That is, there is no undefined behavior, but the values could be anything.

  2. If a POD member is not initialized in the constructor nor via C++11 in-class initialization, it is default-initialized.

  3. The answer is the same regardless of stack or heap.

  4. In C++98 (and not afterward), new int() was specified as performing zero initialization.

Reference: http://en.cppreference.com/w/cpp/language/default_initialization

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • 1
    So default initialization is no-initialization in the case of PODs? – Mustafa Jan 23 '15 at 08:22
  • 1
    @mustafa: Yeah, as per the page I linked "Otherwise, nothing is done: the objects with automatic storage duration (and their subobjects) are initialized to indeterminate values." – John Zwinck Jan 23 '15 at 08:24