Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6991537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:30:59+00:00 2026-05-27T19:30:59+00:00

The following code triggers C4345 on the marked line: #include <array> #include <iostream> int

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:31:00+00:00Added an answer on May 27, 2026 at 7:31 pm

    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

    • VC9 erroneously generates C4345 when compiling value-initialized placement new expressions
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code: #include <stdint.h> int main() { uint8_t Byte; if (Byte < 0)
Why does the following code get the runtime error: Members of the Triggers collection
The following code triggers a segmentation fault at exit. It seems to happen only
I have the following code that triggers two functions whenever a button is clicked.
I have the following code: int result = -1; StringBuilder sb = new StringBuilder();
The following code (simplified example, that triggers the error) doesn't compile with VS 2008:
I'm trying to use Toast inside OnCLickListener . My code triggers the following error:
I have the following code: <TabItem Name=tabItemGeneral> <TabItem.Triggers> <Trigger Property=IsSelected Value=True> <Setter TargetName=tabGeneralImg Property=Source
Consider the following code: <label>Search:</label><asp:TextBox runat=server ID=search ClientIDMode=Static OnKeyUp=$('#searchButton').click(); /><asp:Button runat=server ID=searchButton ClientIDMode=Static />
I would like a standard reference why the following code triggers a compliance warning

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.