Does default constructor zero-initialize member array variable?

Xin Huang

When I examine following program with it's output, I found quite confusing that to get a FrameA object by return value:

  1. when a empty ctor is defined, the member array field is remain uninitialized
  2. when let the compiler generate the ctor, the member array field is initialized to all 0

    auto a = f();       // f() --> return A();
    

Given following SSCCE

#include <cstring>
#include <iostream>
#include <chrono>
#include <algorithm>

using namespace std;

const int MAX = 9999999;

struct FrameA {
  // FrameA() {}
  // FrameA(const FrameA &v) { memcpy(data, v.data, sizeof(data)); }
  char data[1000];
};

FrameA f(int i) { return FrameA(); }

int test(int odd) {
  int sum = 0;
  auto begin = chrono::steady_clock::now();
  for (int i = 0; i < MAX; ++i) {
    auto v = f(odd);
    sum += v.data[0] + v.data[330];
  }
  auto end = chrono::steady_clock::now();
  cout << chrono::duration_cast<chrono::milliseconds>(end - begin).count()
       << " (milliseconds)" << endl;
  return sum;
}

int _tmain(int argc, _TCHAR *argv[]) {
  test(0);
  test(1);
  return 0;
}

When defined an empty ctor, the output is like:

g++ v4.8.1

72 (milliseconds)
73 (milliseconds)

But use compiler generated ctor, the output is:

g++ v4.8.1

1401 (milliseconds)
1403 (milliseconds)

I also tested on VC12, the result is similar.

After examine the assembly, I found when using compiler generated ctor:

  for (int i = 0; i < MAX; ++i) {
    auto v = f(odd);
00A31701  push        3E8h  
00A31706  lea         eax,[ebp-3F8h]  
00A3170C  push        0  
00A3170E  push        eax  
00A3170F  call        _memset (0A32460h)               ;; memset FrameA to 0
    sum += v.data[0] + v.data[330];
00A31714  movsx       eax,byte ptr [ebp-3F8h] 

But using an empty ctor won't call memset to set the array in FrameA to zero.

Is there any explanation to this?

BTW, I searched the C++ 11 draft n3242, but Chap 8.5 zero-initialize and default-initialize seems doesn't covered this case. Is there anything I missed?

Joseph Mansfield

FrameA() will value-initialize the object (§5.2.3/2):

The expression T(), where T is a simple-type-specifier or typename-specifier for a non-array complete object type or the (possibly cv-qualified) void type, creates a prvalue of the specified type, which is value-initialized

Value-initializing a non-union class type that doesn't have a user-provided constructor will zero-initialize it (§8.5/7):

if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.

This zero-initializes each of its members.

Value-initializing a class type that does have a user-provided constructor will simply called the constructor (which in your case, does not initialise the array) (§8.5/7):

if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Explicitly initialize member which does not have a default constructor

From Dev

Initialize static member array to zero

From Dev

cannot find default constructor to initialize member in cpp

From Dev

Zero-Initialize array member in initialization list

From Dev

Zero-initializing an array data member in a constructor

From Dev

Does a default constructor always initialize all members?

From Dev

How do I initialize an array in a default constructor?

From Dev

How to initialize a class member reference to the empty string in the default constructor

From Dev

Initializing member variable without initializer list and a type that does not have a default constructor?

From Dev

Zero initialize multidimentional array in class constructor C++

From Dev

Initialize a 2D array with zero in the constructor C++

From Dev

C++ Class Constructor With Array Member Variable

From Dev

How to initialize template member array from constructor parameter?

From Dev

How to initialize template member array from constructor parameter?

From Dev

Initialize an array member of class when class's copy constructor is deleted

From Dev

How does the compiler initialize local arrays with a default value of zero on the stack?

From Dev

C++ default constructor does not initialize pointer to nullptr?

From Dev

Does std::array default-initialize or value-initialize?

From Dev

How do I invoke non-default constructor for a member variable?

From Java

Zero-initializing elements of a std::array with a default member initializer

From Dev

default constructor for member init?

From Dev

initialize variable in constructor

From Dev

Initialize final variable in constructor

From Dev

Initialize members with deleted default constructor

From Dev

Should T() initialize member variables to zero?

From Dev

Initialize static atomic member variable

From Dev

Initialize member variable once in class

From Dev

Initialize static atomic member variable

From Dev

efficient way to initialize a vector with zero after constructor

Related Related

  1. 1

    Explicitly initialize member which does not have a default constructor

  2. 2

    Initialize static member array to zero

  3. 3

    cannot find default constructor to initialize member in cpp

  4. 4

    Zero-Initialize array member in initialization list

  5. 5

    Zero-initializing an array data member in a constructor

  6. 6

    Does a default constructor always initialize all members?

  7. 7

    How do I initialize an array in a default constructor?

  8. 8

    How to initialize a class member reference to the empty string in the default constructor

  9. 9

    Initializing member variable without initializer list and a type that does not have a default constructor?

  10. 10

    Zero initialize multidimentional array in class constructor C++

  11. 11

    Initialize a 2D array with zero in the constructor C++

  12. 12

    C++ Class Constructor With Array Member Variable

  13. 13

    How to initialize template member array from constructor parameter?

  14. 14

    How to initialize template member array from constructor parameter?

  15. 15

    Initialize an array member of class when class's copy constructor is deleted

  16. 16

    How does the compiler initialize local arrays with a default value of zero on the stack?

  17. 17

    C++ default constructor does not initialize pointer to nullptr?

  18. 18

    Does std::array default-initialize or value-initialize?

  19. 19

    How do I invoke non-default constructor for a member variable?

  20. 20

    Zero-initializing elements of a std::array with a default member initializer

  21. 21

    default constructor for member init?

  22. 22

    initialize variable in constructor

  23. 23

    Initialize final variable in constructor

  24. 24

    Initialize members with deleted default constructor

  25. 25

    Should T() initialize member variables to zero?

  26. 26

    Initialize static atomic member variable

  27. 27

    Initialize member variable once in class

  28. 28

    Initialize static atomic member variable

  29. 29

    efficient way to initialize a vector with zero after constructor

HotTag

Archive