Boost C++ Libraries: Ticket #1217: value_initialized leaves data uninitialized, when using MSVC https://svn.boost.org/trac10/ticket/1217 <p> The data of an instance of boost::value_initialized&lt;T&gt; may not be initialized properly, when using Microsoft Visual C++. I already contacted Fernando Cacciola, the author of value_initialized, and we have developed a workaround, that I would like to propose. </p> <h2 class="section" id="Theproblem">The problem</h2> <p> There are two kinds of types, for which the issue is relevant, for different reasons: </p> <p> When T is an aggregate class or struct that contains both POD and non-POD members, the constructor of value_initialized&lt;T&gt; will typically leave the POD members of T uninitialized, when using MSVC: </p> <pre class="wiki"> #include &lt;boost/utility/value_init.hpp&gt; struct A { std::string s; int i; }; boost::value_initialized&lt;A&gt; a; BOOST_CHECK( get(a).i == 0 ); // Will fail on MSVC! </pre><p> This is caused by a compiler bug, reported by Pavel at the Microsoft Connect website, 7/28/2005, Feedback ID 100744, <a class="ext-link" href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744"><span class="icon">​</span>Value-initialization in new-expression</a> The report was closed at 11/14/2006, and its status was set to "Closed (Won't Fix)". The bug appears relevant to all versions of MSVC, including the next one (MSVC 2008, code name "Orcas"). </p> <p> When T is an array of a POD type, MSVC 2005 provides an interesting warning, within the constructor of the base class of value_initialized&lt;T&gt;: </p> <pre class="wiki">warning C4351: new behavior: elements of array 'T' will be default initialized. </pre><p> This tells us that an older version of the compiler wouldn't properly initialize value_initialized&lt;T&gt;. And indeed the following check fails, when using the 2003 version of MSVC: </p> <pre class="wiki"> typedef int B[2]; boost::value_initialized&lt;B&gt; b; // MSVC 2005 warning C4351: new behavior! BOOST_CHECK( get(b)[1] == 0 ); // Fails on MSVC 2003 (BOOST_MSVC == 1310). </pre><h2 class="section" id="Theproposedworkaround">The proposed workaround</h2> <p> A new version of value_init.hpp is attached to this report, which includes a workaround for MSVC. Basically, when constructing value_initialized&lt;T&gt; it clears the bytes of T, just before constructing T itself. It does so by having the bytes of T stored in an instance of aligned_storage::type, and memsetting those bytes to zero before doing a placement new for type T, at the location of the bytes. </p> <p> The workaround works well for MSVC, because for a built-in type, the value zero always has a bit pattern of only zero's, on any platform supported by this compiler, AFAIK. </p> <p> Note that the workaround itself is a correct and standard compliant implementation of boost::value_initialized, but it may have a small performance penalty in comparison to the original version. </p> <p> -- Niels Dekker <br /><a class="ext-link" href="http://www.xs4all.nl/~nd/dekkerware"><span class="icon">​</span>http://www.xs4all.nl/~nd/dekkerware</a> <br />Scientific programmer at LKEB, Leiden University Medical Center </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1217 Trac 1.4.3 Niels Dekker Mon, 27 Aug 2007 21:41:05 GMT attachment set https://svn.boost.org/trac10/ticket/1217 https://svn.boost.org/trac10/ticket/1217 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">value_init_MSVC_workaround.hpp</span> </li> </ul> <p> Proposes new version of value_init.hpp </p> Ticket Dave Abrahams Wed, 29 Aug 2007 13:29:31 GMT owner set https://svn.boost.org/trac10/ticket/1217#comment:1 https://svn.boost.org/trac10/ticket/1217#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Fernando Cacciola</span> </li> </ul> Ticket Niels Dekker Fri, 31 Aug 2007 21:48:06 GMT <link>https://svn.boost.org/trac10/ticket/1217#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1217#comment:2</guid> <description> <p> Oops, I forgot to do </p> <pre class="wiki"> #include &lt;new&gt; </pre><p> within <a class="missing ticket">value_init_MSVC_workaround.hpp</a> (line 34) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Niels Dekker</dc:creator> <pubDate>Tue, 04 Sep 2007 07:26:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1217#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1217#comment:3</guid> <description> <p> The Microsoft compiler bug regarding <a class="ext-link" href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744"><span class="icon">​</span>Value-initialization in new-expression </a> was reported by Pavel Kuznetsov (MetaCommunications). I would like to have his full name added to <a class="ext-link" href="http://svn.boost.org/trac/boost/attachment/ticket/1217/value_init_MSVC_workaround.hpp#L22"><span class="icon">​</span>value_init_MSVC_workaround.hpp </a> (line 22). </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Niels Dekker</dc:creator> <pubDate>Fri, 07 Sep 2007 22:11:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/1217#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1217#comment:4</guid> <description> <p> I just committed the proposed workaround to the trunk: <a class="ext-link" href="http://svn.boost.org/trac/boost/browser/trunk/boost/utility/value_init.hpp?rev=39157"><span class="icon">​</span>value_init.hpp - SVN revision 39157</a> </p> <p> See also the Boost Developers mailing list: <a class="ext-link" href="http://lists.boost.org/Archives/boost/2007/08/126759.php"><span class="icon">​</span>{utility} Proposal MSVC workaround for value_initialized - Ticket #1217</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Fernando Cacciola</dc:creator> <pubDate>Fri, 02 Nov 2007 23:13:29 GMT</pubDate> <title>status, milestone changed; resolution set https://svn.boost.org/trac10/ticket/1217#comment:5 https://svn.boost.org/trac10/ticket/1217#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.35.0</span> </li> </ul> Ticket