<div dir="ltr"><div><div><div><div>Hello Cyril,<br><br></div>Thank you for pointing out this behavior. This is a memory allocation problem in your program. To need to create some memory space to save the data that is returned by the vnl_matrix.<br></div>See below a way to solve your issue.<br></div><br>Hope this helps,<br></div>Francois<br><div><div><br>#include <iostream><br>#include <vnl_vector.h><br>#include <vnl_matrix.h><br>#include <cstring><br><br>int main()<br>{<br>  vnl_matrix<double> matrix(2, 2);<br>  matrix.put(0, 0, 4);<br>  matrix.put(0, 1, 3);<br>  matrix.put(1, 0, 2);<br>  matrix.put(1, 1, 1);<br><br>  // Display the matrix<br>  std::cout << "Matrix" << std::endl;<br>  std::cout << matrix << std::endl;<br><br>  // Display the first row of the matrix,<br>  vnl_vector<double> matrixRow;<br>  matrixRow = matrix.get_row(0);<br>  std::cout << "First row, stored in a vnl_vector" << std::endl;<br>  std::cout << matrixRow << std::endl;<br><br>  double matrixRowData[2];<br>  std::memcpy(matrixRowData,  matrix.get_row(0).data_block(), 2*sizeof(double));<br>  std::cout << "First row, stored in a double*" << std::endl;<br>  std::cout << matrixRowData[0] << " " << matrixRowData[1] << std::endl;<br><br>  double* matrixRowDataFromVector = matrixRow.data_block();<br>  std::cout << "First row, stored in a double* after passing through a vnl_vector" << std::endl;<br>  std::cout << matrixRowDataFromVector[0] << " " << matrixRowDataFromVector[1] << std::endl;<br><br>  return 0;<br>}<br><br>

<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">Matrix</span></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">4 3 </span></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">2 1 </span></p>
<p style="margin:0px;text-indent:0px;font-family:"monospace";font-size:9pt;color:rgb(0,0,0)"><br></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">First row, stored in a vnl_vector</span></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">4 3</span></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">First row, stored in a double*</span></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">4 3</span></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">First row, stored in a double* after passing through a vnl_vector</span></p>
<p style="margin:0px;text-indent:0px"><span style="font-family:"monospace";font-size:9pt;color:rgb(0,0,0)">4 3</span></p><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Nov 4, 2016 at 10:26 AM, Cyril Mory <span dir="ltr"><<a href="mailto:cyril.mory@creatis.insa-lyon.fr" target="_blank">cyril.mory@creatis.insa-lyon.fr</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
  
    
  
  <div bgcolor="#FFFFFF" text="#000000">
    Hello, ITK users,<br>
    <br>
    I've encountered a behavior with vnl_matrix, and I'm wondering
    whether it is a bug or a feature. The following code outputs
    differents results:<br>
    <br>
    #include <iostream><br>
    #include <vnl_vector.h><br>
    #include <vnl_matrix.h><br>
     <br>
    int main()<br>
    {<br>
      vnl_matrix<double> matrix(2, 2);<br>
      matrix.put(0, 0, 4);<br>
      matrix.put(0, 1, 3);<br>
      matrix.put(1, 0, 2);<br>
      matrix.put(1, 1, 1);<br>
    <br>
      // Display the matrix<br>
      std::cout << "Matrix" << std::endl;<br>
      std::cout << matrix << std::endl;<br>
    <br>
      // Display the first row of the matrix,<br>
      vnl_vector<double> matrixRow;<br>
      matrixRow = matrix.get_row(0);<br>
      std::cout << "First row, stored in a vnl_vector" <<
    std::endl;<br>
      std::cout << matrixRow << std::endl;<br>
    <br>
      double* matrixRowData = matrix.get_row(0).data_block()<wbr>;<br>
      std::cout << "First row, stored in a double*" <<
    std::endl;<br>
      std::cout << matrixRowData[0] << " " <<
    matrixRowData[1] << std::endl;<br>
    <br>
      double* matrixRowDataFromVector = matrixRow.data_block();<br>
      std::cout << "First row, stored in a double* after passing
    through a vnl_vector" << std::endl;<br>
      std::cout << matrixRowDataFromVector[0] << " "
    << matrixRowDataFromVector[1] << std::endl;<br>
    <br>
      return EXIT_SUCCESS;<br>
    }<br>
    <br>
    Output : <br>
    
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">Matrix</span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">4 3 </span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">2 1 </span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px;font-family:'Monospace';font-size:9pt;color:#31363b">
</p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">First row, stored in a vnl_vector</span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">4 3</span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">First row, stored in a double*</span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">0 3</span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">First row, stored in a double* after passing through a vnl_vector</span></p>
    <p style="margin-top:0px;margin-bottom:0px;margin-left:0px;margin-right:0px;text-indent:0px"><span style="font-family:'Monospace';font-size:9pt;color:#31363b">4 3</span></p>
    
    <br>
    <br>
    It was compiled with the following CMakeLists.txt:<br>
    <br>
    <br>
    cmake_minimum_required(VERSION 2.8)<br>
     <br>
    project(IsThisAvnlBug)<br>
     <br>
    find_package(ITK REQUIRED)<br>
    include(${ITK_USE_FILE})<br>
     <br>
    add_executable(IsThisAvnlBug MACOSX_BUNDLE IsThisAvnlBug.cxx)<br>
    target_link_libraries(<wbr>IsThisAvnlBug ${ITK_LIBRARIES})<br>
    <br>
  </div>

<br>______________________________<wbr>_______<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Visit other Kitware open-source projects at<br>
<a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>opensource/opensource.html</a><br>
<br>
Kitware offers ITK Training Courses, for more information visit:<br>
<a href="http://www.kitware.com/products/protraining.php" rel="noreferrer" target="_blank">http://www.kitware.com/<wbr>products/protraining.php</a><br>
<br>
Please keep messages on-topic and check the ITK FAQ at:<br>
<a href="http://www.itk.org/Wiki/ITK_FAQ" rel="noreferrer" target="_blank">http://www.itk.org/Wiki/ITK_<wbr>FAQ</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/insight-users" rel="noreferrer" target="_blank">http://public.kitware.com/<wbr>mailman/listinfo/insight-users</a><br>
<br></blockquote></div><br></div>