20

The following code triggers C4345 on the marked line:

#include <array>
#include <iostream>

int main(){
    static unsigned const buf_size = 5;
    typedef std::array<char, buf_size> buf_type;

    char buf[] = { 5, 5, 5, 5, 5 };
    void* p = &buf[0];
    buf_type* pbuf = new (p) buf_type(); // <=== #10

    for(unsigned i=0; i < buf_size; ++i)
        std::cout << (char)((*pbuf)[i] + 0x30) << ' ';
}

main.cpp(10): warning C4345: behavior change: an object of POD type constructed with an initializer of the form () will be default-initialized

So, according to their warning, line 10 should have the same behaviour as if it was written as

buf_type* pbuf = new (p) buf_type; // note the missing '()'

However, the output differes. Namely, the first version will print five 0s, while the second version will print five 5s. As such, the first version is indeed value-initialized (and the underlying buffer zero-initialized), even though MSVC says it won't.

Can this be considered a bug in MSVC? Or did I misinterpret the warning / is my test code faulty?

1
  • 9
    ¤ The warning refers to lack of initialization in earlier versions, i.e. it's a warning, Warning, WARNING!, we've fixed an earlier bug! Quoting the holy Standard: "An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized", which in the absence of specified initialization for something devolves to zero-initialization. The warning, however, incorrectly refers to "default-initialization" (incorrect unless there's still a bug). That may be why the answers here so far have been pretty confused. Cheers & hth., Jan 2, 2012 at 3:50

3 Answers 3

13

TL;DR version: MSVC's behavior is actually correct, although the warning is incorrect (it should say value-initialized).


For new (p) buf_type;, MSVC is correct to perform default initialization, because the standard (5.3.4 [expr.new]) demands:

A new-expression that creates an object of type T initializes that object as follows:

  • If the new-initializer is omitted, the object is default-initialized (8.5); if no initialization is performed, the object has indeterminate value.
  • Otherwise, the new-initializer is interpreted according to the initialization rules of 8.5 for direct-initialization.

std::array is a class type. For class types (8.5 [dcl.init]):

To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type, the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

default-initialization leaves the memory unchanged only for primitive types (and raw arrays thereof)

On the other hand, std::array has a defaulted default constructor, so the members themselves ought to be default-initialized. And in fact you observed that.


Then according to the same section, the new (p) buf_type(); version causes direct-initialization.

std::array is an aggregate, so I think this rule (8.5.1 [dcl.init.aggr]) then applies:

If there are fewer initializer-clauses in the list than there are members in the aggregate, then each member not explicitly initialized shall be initialized from an empty initializer list (8.5.4).

And that means value-initialization for all elements.

Nope, here's the rule (8.5 [dcl.init]):

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

Value initialization of an aggregate means value initialization of all elements, since the elements are primitive, that means zero fill.

So MSVC's behavior is actually correct, although the warning is incorrect (it should say value-initialized).

It's already been reported, see

5
  • And the default ctor of std::array is implicitly defined, there rules are somewhere under §12.1 IIRC. An implicitly defined default ctor has an empty ctor initializer list and compound statement (aka name() {}). As such, the array should be left uninitialized in the new (p) buffer_type; case (it is), and in the first version according to the warning (it is not). If what you quoted would indeed initialize the array, then version 2 would also print all zeros.
    – Xeo
    Jan 2, 2012 at 3:35
  • On the part below the splitter: Yes, but the warning says that it will default initialize, which it does not. This question is specifically about the behaviour of MSVC in that regard.
    – Xeo
    Jan 2, 2012 at 3:53
  • "MSVC's behavior is actually correct, although the warning is incorrect (it should say value-initialized)." +1 This is the correct answer and should stand out more; the rest is mostly extraneous. ;-]
    – ildjarn
    Jan 2, 2012 at 4:17
  • @ildjarn Please see my answer, the warning is correct. "Default initialized" means it will call its default constructor, whereas it would default to not do that with a POD data type. Without the () it would not initialize the POD type at all, it would just make a pointer to an object-of-type-POD at that location, without changing the memory at that location. Jan 2, 2012 at 4:34
  • @OrgnlDave: No one cares what you say "default initialization" means. We're using that term according to the definition in the standard.
    – Ben Voigt
    Jan 2, 2012 at 5:10
1

Here is how I read the MS article http://msdn.microsoft.com/en-us/library/wewb47ee%28v=vs.80%29.aspx

Under the old version of VS (your original syntax) - in this article, VS 2003 - a POD initialize would default initialize with 0s. Your code would support this.

Under the new version of VS - in this article, that would be VS 2005 - it was up to the programmer to initializes the POD explicitly as no default initialization was performed. In your case, as shown in your code, it correctly displays 5s.

Thus, as I read it, the old syntax initialized the POD to 0s even if your code had already explicitly initialized it. I do add a disclaimer that it is very late here and I am tired, so this may all be babbling.

2
  • You're totally mixing up default and value initialization here.
    – Xeo
    Jan 2, 2012 at 3:39
  • @Xeo: No, Microsoft is totally mixing up default and value initialization. Read the comments inside the sample code at that link.
    – Ben Voigt
    Jan 2, 2012 at 4:06
-1

This warning refers strictly to a change that occurred in the way Visual Studio would interpret and compile that statement from a previous version. "Behavior change" means that "if you wrote this with an older VC++, watch out, things have changed." It is not meant to say that the first example is equivalent to the second.

In fact, this is the compiler conforming closer to the C++ standard. It isn't a warning that those things WILL be equivalent - it is a warning that they ONCE WERE but now, aren't. You extrapolated what "default initialized" means incorrectly.

Normally, a POD object that is placement-new'd SHOULD NOT be default initialized, so this warning is correct in telling you that () will make it happen. However, it isn't a warning against doing this per se, but as I said, Microsoft noting a change.

An std::array is guaranteed (so says MSDN documentation) to be a POD type when the type supplied is POD (such as char). A POD type is basically plain-old-data: the compiler treats it as if it were just an object. Even though it is a class, no initialization is performed on it unless explicitly called for, the same as it wouldn't initialize a standard C array pointer (and, in fact, cannot).

Let's go to some C code for some illumination.

// POD version.
// buf_type = new (p) buf_type;
typedef char buf_type;
buf_type *pbuf = p;             // Pointer is assigned

And,

// Constructed version.
// buf_type = new (p) buf_type();
void construct_buf_type(buf_type *what);

typedef char buf_type;
buf_type *pbuf = p;             // Pointer is assigned
construct_buf_type(buf_type);   // Constructor is called which default-initializes it

What happens behind the scenes is a little bit different, but this is conceptually exactly what happens. Both statements tell the compiler to place the object at that location; the one with the () at the end tells it to call the default constructor afterwards.

Yes, this can be an annoying syntactic ambiguity if you forget your ()'s in your new's when you're writing POD objects. But almost nobody ever does, which is part of why PODs are often mis-understood.

7
  • 1
    As @Xeo told Gerald, you're mixing up default and value initialization. And -1 because I've already quoted the relevant sections of the standard, you didn't bother to read my answer. buf_type = new (p) buf_type; does default-initialize the object, but buf_type = new (p) buf_type(); value-initializes it.
    – Ben Voigt
    Jan 2, 2012 at 5:07
  • @BenVoigt I'm not sure how you can get this mixed up... buf_type = new (p) buf_type; does not initialize the object, all it does is tell the compiler to treat that section of memory as the object...it is neither default initialization nor value initialization without () Jan 2, 2012 at 7:26
  • 1
    Read the very first quote block in my answer. It's straight from the standard. The new-initializer is omitted, so the object is default-initialized. Period. End of story. The standard itself says so.
    – Ben Voigt
    Jan 2, 2012 at 7:29
  • BTW that means in this case: (1) The default constructor is called (2) The default constructor does nothing (3) The memory is unchanged.
    – Ben Voigt
    Jan 2, 2012 at 7:33
  • @BenVoigt I give up, you're correct. I found the relevant part of the standard. From section 8.5 item 5 in the standard: "To default-initialize an object of type T means: ... [if the T is POD] the object is zero-initialized." Would've solved a lot to find that before. I looked further today because I got -2 rep on this yesterday, is that because of the +1 on your comments? Jan 31, 2012 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.