Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let me flesh out your question a little bit. You mention:</p> <blockquote> <p>I ask because I want to return an array [...] from a function but I want to build the array without knowing the size beforehand. The size will be passed as an argument. I want to know if there is a way to do this without using dynamic allocation.</p> </blockquote> <p>For the <em>I want to return an array from a function [...] size passed as an argument</em>, it seems reasonable to me that you can use <code>std::vector</code> everywhere, and call its <code>.data()</code> method when you need access to the underlying array (which is guaranteed to be contiguous). For example:</p> <pre><code>std:vector&lt;double&gt; myfun(size_t N) { std::vector&lt;double&gt; r(N); // fill r[0], r[1], ..., r[N-1] return r; } // later on: r.data(); // gives you a pointer to the underlying double[N] </code></pre> <p>And for the <em>I want to to do this without dynamic allocation</em>, that is not possible unless you know the size at compile time. If that is the case, then do exactly as before but use <code>std::array</code>, which can implement optimizations based on known compile-time size:</p> <pre><code>std::array&lt;double, N&gt; myfun() { std::array&lt;double, N&gt; r; // fill r[0], r[1], ..., r[N-1] return r; } // later on: r.data(); // gives you a pointer to the underlying double[N] </code></pre> <p>And to be generic, I would actually use a template function capable of working with arbitrary containers:</p> <pre><code>template&lt;typename T&gt; void myfun(T&amp; data) { for(int k=0; k&lt;data.size(); k++) { // do stuff to data[k] } } // call as, for example: std::vector&lt;double&gt; data(10); myfun(data); // or equally valid: std::array&lt;double, 10&gt; data; myfun(data); </code></pre> <p>Finally, if you are working with two-dimensional data, please remember that when you store the Matrix in row-major order that is:</p> <pre><code>Matrix [1, 2; 3 4] is stored as [1 2 3 4] </code></pre> <p>then you can refer to element <code>(i, j)</code> of the matrix by calling <code>data[i * ncols + j]</code>. For example: consider a three by four matrix:</p> <pre><code>a b c d e f g h i j k l </code></pre> <p>The element <code>(2, 2)</code> (that is: third row, third column because we assume zero-based C-type indexing) is calculated as: <code>M[2][2] = M[2 * 4 + 2] = M[10] = k</code>. This is the case because it was stored as:</p> <pre><code> [a b c d e f g h i j k l] [0 1 2 3 4 5 6 7 8 9 10 11] </code></pre> <p>and <code>k</code> is the element with index <code>10</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload