Default Initialization Versus Zero Initialization

前端 未结 4 566
Happy的楠姐
Happy的楠姐 2020-12-20 05:14

I cannot understand the behavior of gcc 4.8.1 or Visual Studio 2015 with respect to default initialization versus value initialization.

It doesn\'t help that I\'m tr

相关标签:
4条回答
  • 2020-12-20 05:48
    Foo foo;
    

    This default-initializes foo, and since Foo's default constructor is trivial, it effectively doesn't initialize it at all, so foo._bar can hold any value (including 0).

    Foo()
    

    This value-initializes the temporary object, which in case of trivial default constructor means zero-initialization, so Foo()._bar is equal to 0.

    0 讨论(0)
  • 2020-12-20 05:55

    The logic is quite simple:

    1. Default initialization of a class just default initializes all members.
    2. Default initialization of built-in types leaves member uninitialized.
    3. Accessing an uninitialized object yields undefined behavior.
    4. Undefined behavior can do anything it wants.
    5. Both compilers provide "correct" results. Note that causing nasal demons to be emitted would also be correct.
    0 讨论(0)
  • 2020-12-20 06:05

    Default initialisation, of classes like this without user-defined constructors, does nothing, leaving each trivial member with an indeterminate value.

    Value initialisation will zero-initialise each member.

    In the first case, you're printing:

    • the indeterminate value of a default-initialised Foo foo;
    • the zero value of a value-initialised Foo()
    • the indeterminate value of a default-initialised bar b;

    The third one happens to be zero; perhaps because it reuses the storage of the temporary value-initialised Foo.

    In the second case, you're printing the indeterminate values of two default-initialised objects. Coincidentally, they have zero values in one case but not the other.

    Both programs have undefined behaviour, since they use uninitialised values.

    0 讨论(0)
  • 2020-12-20 06:13

    n3376 quotes

    8.5/11

    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. [ Note: Objects with static or thread storage duration are zero-initialized, see 3.6.2. — end note ]

    8.5/6

    To default-initialize an object of type T means: if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

    8.5/10

    An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

    8.5/7

    To value-initialize an object of type T means:

    ...

    otherwise, the object is zero-initialized.

    8.5/5

    To zero-initialize an object or reference of type T means: if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;

    So, in your case, there are nor static storage duration variables, nor thread-local variables, so objects foo and b will be default-initialized, that means, that constructor will be called. Default-constructor (not user-defined) will not initialize members and in members will be arbitrary garbage and this arbitrary garbage may be 0 (thanks to Jarod42 for point this in comment). And Foo().printBar(); should print 0, since object is zero-initialized.

    0 讨论(0)
提交回复
热议问题