Boost C++ Libraries: Ticket #1531: segfault in dynamic_bitset::reference assign operator https://svn.boost.org/trac10/ticket/1531 <p> I will of course take a look at this later (i.e., I've go to stop here and do the work I get paid to do), but for now, this will serve as a heads up to any one who's interested. Perhaps I'm not initializing <code>bset</code> correctly, but I couldn't easily tell that from the headers or the online documentation. </p> <p> test code: </p> <pre class="wiki">$ cat bitset.cpp #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;boost/dynamic_bitset.hpp&gt; int main() { boost::dynamic_bitset&lt;&gt; bset; bset[0] = true; bset[0] = false; return 0; } </pre><p> compile + gdb session: </p> <pre class="wiki">$ g++ -I ~/local/include/boost-1_35/ -o bitset bitset.cpp $ ./bitset Segmentation fault $ g++ -g -I ~/local/include/boost-1_35/ -o bitset bitset.cpp $ gdb bitset GNU gdb Red Hat Linux (6.5-15.fc6rh) Copyright (C) 2006 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "i386-redhat-linux-gnu"...Using host libthread_db library "/lib/libthread_db.so.1". (gdb) r Starting program: /users/bcollins/test/C++/bitset Program received signal SIGSEGV, Segmentation fault. 0x08048877 in boost::dynamic_bitset&lt;unsigned long, std::allocator&lt;unsigned long&gt; &gt;::reference::do_set (this=0xbf856240) at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:115 115 void do_set() { m_block |= m_mask; } (gdb) bt #0 0x08048877 in boost::dynamic_bitset&lt;unsigned long, std::allocator&lt;unsigned long&gt; &gt;::reference::do_set (this=0xbf856240) at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:115 #1 0x080488a3 in boost::dynamic_bitset&lt;unsigned long, std::allocator&lt;unsigned long&gt; &gt;::reference::do_assign (this=0xbf856240, x=true) at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:118 #2 0x080488d1 in boost::dynamic_bitset&lt;unsigned long, std::allocator&lt;unsigned long&gt; &gt;::reference::operator= (this=0xbf856240, x=true) at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:103 #3 0x080486c9 in main () at bitset.cpp:9 (gdb) f 0 #0 0x08048877 in boost::dynamic_bitset&lt;unsigned long, std::allocator&lt;unsigned long&gt; &gt;::reference::do_set (this=0xbf856240) at /users/bcollins/local/include/boost-1_35/boost/dynamic_bitset/dynamic_bitset.hpp:115 115 void do_set() { m_block |= m_mask; } (gdb) p m_block $1 = (long unsigned int &amp;) @0x0: Cannot access memory at address 0x0 (gdb) </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/1531 Trac 1.4.3 smckay@… Wed, 16 Jan 2008 17:34:46 GMT <link>https://svn.boost.org/trac10/ticket/1531#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/1531#comment:1</guid> <description> <p> A new dynamic_bitset with no size is empty. You're trying to assign to an empty vector. </p> <p> I think it would work if you initialized bset with a size of 1. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Gennaro Prota</dc:creator> <pubDate>Mon, 07 Jul 2008 19:19:30 GMT</pubDate> <title>status changed; cc, resolution set https://svn.boost.org/trac10/ticket/1531#comment:2 https://svn.boost.org/trac10/ticket/1531#comment:2 <ul> <li><strong>cc</strong> <span class="trac-author">gennaro.prota@…</span> added </li> <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">invalid</span> </li> </ul> <p> This isn't a bug: as noted in the previous comment, the size of <code>bset</code> is zero, and evaluating <code>bset[0]</code> violates the precondition <code>n &lt; this-&gt;size()</code>, which holds for both the <code>operator[]()</code> members, and thus invokes <strong>undefined behavior</strong>. </p> Ticket