Note that there are some explanatory texts on larger screens.

plurals
  1. POMemory footprint of C++ functions returning by reference or value?
    primarykey
    data
    text
    <p>I am trying to reduce the memory footprint of an application developed mostly in C++ and one the things I was looking into was returning a particularly large data structure by reference vs returning them by value. I needed some confirmation on it. For instance, say I have member functions in a class <code>some_object</code> as follows: </p> <pre><code>const some_type&amp; get_some_type(...) { ...; return ...; } </code></pre> <p>vs </p> <pre><code>const some_type get_some_type(...) { ...; return ...; } </code></pre> <p>Is my understanding correct that assignment by calling the latter (return by value) would leave two copies of the data <code>some_type</code> in memory at certain points during the application's execution, while calling the former (return by reference) avoids this? </p> <p>Another effort as part of the overall goal of reducing the memory footprint was to change the very definition of <code>some_type</code> in a manner that would preclude using the return by reference option above. In such a case, since <code>some_type</code> (which is actually a container of sorts, say, of data of type <code>data_type</code>) is used inside a loop as follows: </p> <pre><code>const some_type&amp; = some_object-&gt;get_some_type(); // returns by ref. a large container for(...) { ...; data_type = some_type.at(...); ...; } </code></pre> <p>I assume if we are constrained to use the return by value above, then I would need to make the change as follows by introducing a new function <code>get_some_type_at</code> to get an element of the container by value (are there any downsides to doing this that I should be aware of, say, in performance?):</p> <pre><code>for(...) { ...; data_type = some_object-&gt;get_some_type_at(...); // returns by value a small object ...; } </code></pre> <p>Again I am looking mostly for confirmation of these aspects. But details and insights are most appreciated. Thanks for your time and interest!</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. 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