title: memory issue of copying C++ vectors
body: <p>I somehow fixed a memory issue in my code but am not quite sure what actually happens and I would like to see if anyone could possibly give me a reasonable explanation.</p>
  
  <p>The code involves doing some computation with a class <code>ANN_Force</code>, initially when setting up parameter <code>values_of_biased_nodes</code>, I use following code:</p>
  
  <pre><code>values_of_biased_nodes = bias;
  </code></pre>
  
  <p>and that gives me following memory error:</p>
  
  <pre><code>Program received signal SIGSEGV, Segmentation fault.
  __memmove_avx_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-avx-unaligned.S:138
  138 ../sysdeps/x86_64/multiarch/memcpy-avx-unaligned.S: No such file 
  or directory.
  
  #0  __memmove_avx_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-avx-unaligned.S:138
  #1  0x00007ffff7a0c903 in std::vector&lt;double, std::allocator&lt;double&gt; &gt;::operator=(std::vector&lt;double, std::allocator&lt;double&gt; &gt; const&amp;) () from /home/kengyangyao/.openmm/lib/libOpenMM.so
  #2  0x00007ffff7a5bdcf in std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt;::operator=(std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt; const&amp;) () from /home/kengyangyao/.openmm/lib/libOpenMM.so
  #3  0x00007ffff76a3122 in OpenMM::ANN_Force::set_values_of_biased_nodes (this=0x6480e0, bias=...)
  at /home/kengyangyao/Dropbox/temp_Linux/ANN_Force/openmmapi/src/ANN_Force.cpp:57
  #4  0x000000000040cd4a in test_forward_and_backward_prop () at test_ANN_package.cpp:110
  #5  0x000000000041468b in main (argc=1, argv=0x7fffffffdae8) at test_ANN_package.cpp:598
  </code></pre>
  
  <p>When I added a line:</p>
  
  <pre><code>values_of_biased_nodes.resize(bias.size());
  values_of_biased_nodes = bias;
  </code></pre>
  
  <p>The problem disappeared but I have no idea why.  My guess would be without <code>resize</code>, <code>values_of_biased_nodes = bias;</code> simply moves whatever in <code>bias</code> directly to <code>values_of_biased_nodes</code> without checking if the memory near <code>values_of_biased_nodes</code> is large enough to hold the values.  I wonder if anyone could help me figure it out?  Also I noticed that in some machines it works without <code>resize</code>, is it due to different memory management strategies, or simply because those machines have large enough memory?</p>
  
  <p>Thank you!</p>
  
  <p>UPDATED: I ran it with valgrind, here is output:</p>
  
  <pre><code>==13494== Memcheck, a memory error detector
  ==13494== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
  ==13494== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
  ==13494== Command: ./test_ANN_package
  ==13494== 
  /home/kengyangyao/.openmm/lib/plugins
  running test_forward_and_backward_prop
  ==13494== Conditional jump or move depends on uninitialised value(s)
  ==13494==    at 0x4F9689E: std::vector&lt;double, std::allocator&lt;double&gt; &gt;::operator=(std::vector&lt;double, std::allocator&lt;double&gt; &gt; const&amp;) (in /home/kengyangyao/.openmm/lib/libOpenMM.so)
  ==13494==    by 0x4FE5DCE: std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt;::operator=(std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt; const&amp;) (in /home/kengyangyao/.openmm/lib/libOpenMM.so)
  ==13494==    by 0x5374121: OpenMM::ANN_Force::set_values_of_biased_nodes(std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt;) (ANN_Force.cpp:57)
  ==13494==    by 0x40CD49: test_forward_and_backward_prop() (test_ANN_package.cpp:110)
  ==13494==    by 0x41468A: main (test_ANN_package.cpp:598)
  ==13494==  Uninitialised value was created by a heap allocation
  ==13494==    at 0x4C2E0EF: operator new(unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==13494==    by 0x41BACF: __gnu_cxx::new_allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;::allocate(unsigned long, void const*) (new_allocator.h:104)
  ==13494==    by 0x41ACA5: std::allocator_traits&lt;std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::allocate(std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt;&amp;, unsigned long) (alloc_traits.h:491)
  ==13494==    by 0x419809: std::_Vector_base&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::_M_allocate(unsigned long) (stl_vector.h:170)
  ==13494==    by 0x41904A: std::_Vector_base&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::_M_create_storage(unsigned long) (stl_vector.h:185)
  ==13494==    by 0x41728C: std::_Vector_base&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::_Vector_base(unsigned long, std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; const&amp;) (stl_vector.h:136)
  ==13494==    by 0x4155AB: std::vector&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;, std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; &gt;::vector(unsigned long, std::allocator&lt;std::__cxx11::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; &gt; const&amp;) (stl_vector.h:278)
  ==13494==    by 0x415072: OpenMM::ANN_Force::ANN_Force() (in /home/kengyangyao/Dropbox/temp_Linux/ANN_Force/openmmapi/tests/test_ANN_package)
  ==13494==    by 0x40C8AA: test_forward_and_backward_prop() (test_ANN_package.cpp:103)
  ==13494==    by 0x41468A: main (test_ANN_package.cpp:598)
  ==13494== 
  ==13494== Invalid write of size 1
  ==13494==    at 0x4C3245C: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==13494==    by 0x4F96902: std::vector&lt;double, std::allocator&lt;double&gt; &gt;::operator=(std::vector&lt;double, std::allocator&lt;double&gt; &gt; const&amp;) (in /home/kengyangyao/.openmm/lib/libOpenMM.so)
  ==13494==    by 0x4FE5DCE: std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt;::operator=(std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt; const&amp;) (in /home/kengyangyao/.openmm/lib/libOpenMM.so)
  ==13494==    by 0x5374121: OpenMM::ANN_Force::set_values_of_biased_nodes(std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt;) (ANN_Force.cpp:57)
  ==13494==    by 0x40CD49: test_forward_and_backward_prop() (test_ANN_package.cpp:110)
  ==13494==    by 0x41468A: main (test_ANN_package.cpp:598)
  ==13494==  Address 0x3f947ae147ae147b is not stack'd, malloc'd or (recently) free'd
  ==13494== 
  ==13494== 
  ==13494== Process terminating with default action of signal 11 (SIGSEGV)
  ==13494==  General Protection Fault
  ==13494==    at 0x4C3245C: memcpy@GLIBC_2.2.5 (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
  ==13494==    by 0x4F96902: std::vector&lt;double, std::allocator&lt;double&gt; &gt;::operator=(std::vector&lt;double, std::allocator&lt;double&gt; &gt; const&amp;) (in /home/kengyangyao/.openmm/lib/libOpenMM.so)
  ==13494==    by 0x4FE5DCE: std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt;::operator=(std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt; const&amp;) (in /home/kengyangyao/.openmm/lib/libOpenMM.so)
  ==13494==    by 0x5374121: OpenMM::ANN_Force::set_values_of_biased_nodes(std::vector&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt;, std::allocator&lt;std::vector&lt;double, std::allocator&lt;double&gt; &gt; &gt; &gt;) (ANN_Force.cpp:57)
  ==13494==    by 0x40CD49: test_forward_and_backward_prop() (test_ANN_package.cpp:110)
  ==13494==    by 0x41468A: main (test_ANN_package.cpp:598)
  ==13494== 
  ==13494== HEAP SUMMARY:
  ==13494==     in use at exit: 2,016,954 bytes in 7,600 blocks
  ==13494==   total heap usage: 9,419 allocs, 1,819 frees, 2,218,864 bytes allocated
  ==13494== 
  ==13494== LEAK SUMMARY:
  ==13494==    definitely lost: 0 bytes in 0 blocks
  ==13494==    indirectly lost: 0 bytes in 0 blocks
  ==13494==      possibly lost: 0 bytes in 0 blocks
  ==13494==    still reachable: 2,016,954 bytes in 7,600 blocks
  ==13494==                       of which reachable via heuristic:
  ==13494==                         stdstring          : 196 bytes in 6 blocks
  ==13494==         suppressed: 0 bytes in 0 blocks
  ==13494== Reachable blocks (those to which a pointer was found) are not shown.
  ==13494== To see them, rerun with: --leak-check=full --show-leak-kinds=all
  ==13494== 
  ==13494== For counts of detected and suppressed errors, rerun with: -v
  ==13494== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
  [1]    13494 segmentation fault (core dumped)  valgrind --leak-check=yes --track-origins=yes ./test_ANN_package
  </code></pre>
  
  <p>seems that some memory is not correctly handled?</p>