title: Using valuePtr() etc to allocate space for Eigen3 SparseMatrix
body: <p>I am trying to initialize <code>Eigen::SparseMatrix A</code> by using <code>malloc</code> like this</p>
  
  <pre><code>A.valuePtr() = static_cast&lt;T*&gt;(std::malloc(nnz*sizeof(T)));
  A.innerIndexPtr() = static_cast&lt;int*&gt;(std::malloc(nnz*sizeof(int)));
  </code></pre>
  
  <p>but I get the error</p>
  
  <pre><code>error: lvalue required as left operand of assignment
  </code></pre>
  
  <p>for both statements. In case it matters, the function containing the lines takes a <code>Eigen::SparseMatrix&lt;T, Eigen::RowMajor&gt;</code> by reference.</p>
  
  <p>Can anyone help me with this? I'm using g++ 5.2.</p>
  
  <p>EDIT:
  The function <code>valuePtr</code> of class <code>SparseMatrix</code> is</p>
  
  <pre><code>inline Scalar* valuePtr() { return &amp;m_data.value(0); }
  </code></pre>
  
  <p><code>Scalar</code> is a template paramter. <code>m_data</code> is a protected variable of type <code>CompressedStorage</code> whose method <code>value(size_t i)</code> returns a <strong>reference</strong> to the ith member of its internal data array.</p>
  
  <pre><code> inline Scalar&amp; value(size_t i) { return m_values[i]; }
  </code></pre>
  
  <p>So, I concluded that <code>valuePtr()</code> returns the address of the first element of an array. I should, then, be able to allocate space for that array by malloc.</p>
  
  <p>In case someone is interested, I include a link for reference - please see lines after 84, and 131. <a href="http://eigen.tuxfamily.org/dox/SparseMatrix_8h_source.html" rel="nofollow">Eigen3 SparseMatrix class</a></p>