Note that there are some explanatory texts on larger screens.

plurals
  1. POCoding practice: return by value or by reference in Matrix multiplication?
    text
    copied!<p>I'm writing this question with reference to <a href="https://stackoverflow.com/questions/475888/will-this-lead-to-a-memory-leak-in-c">this one</a> which I wrote yesterday. After a little documentation, it seems clear to me that what I wanted to do (and what I believed to be possible) is nearly impossible if not impossible at all. There are several ways to implement it, and since I'm not an experienced programmer, I ask you which choice would you take. I explain again my problem, but now I have some solutions to explore.</p> <p><strong>What I need</strong></p> <p>I have a Matrix class, and I want to implement multiplication between matrices so that the class usage is very intuitive:</p> <pre><code>Matrix a(5,2); a(4,1) = 6 ; a(3,1) = 9.4 ; ... // And so on ... Matrix b(2,9); b(0,2) = 3; ... // And so on ... // After a while Matrix i = a * b; </code></pre> <p><strong>What I had yesterday</strong></p> <p>At the moment I overloaded the two operators <code>operator*</code> and <code>operator=</code> and until yesterday night the were defined in this way:</p> <pre><code>Matrix&amp; operator*(Matrix&amp; m); Matrix&amp; operator=(Matrix&amp; m); </code></pre> <p>The operator* instantiates a new Matrix object (<code>Matrix return = new Matrix(...)</code>) on the heap, set the values and then just:</p> <pre><code>return *result; </code></pre> <p><strong>What I have today</strong></p> <p>After the <a href="https://stackoverflow.com/questions/475888/will-this-lead-to-a-memory-leak-in-c">discussion</a> I decided to implement it in a "different way" to avoid the user to be bothered bother by pointers of any type and to keep the usage unchanged. The "different way" is to pass the returning value of operator* by value:</p> <pre><code>Matrix operator*(Matrix&amp; m); Matrix&amp; operator=(Matrix&amp; m); </code></pre> <p>The operator* instantiates <code>return</code> on the stack, set the values and then return the object.</p> <p>There is a problem with this approach: it doesn't work. The operator= expects a Matrix&amp; and operator* returns a Matrix. Moreover this approach doesn't look so good to me for another reason: I'm dealing with matrices, that can be very large and the aims of this library were to be 1) good enough for my project 2) fast, so probably passing by value should not be an option.</p> <p><strong>Which solutions I have explored</strong></p> <p>Well, following the suggestions in the previous <a href="https://stackoverflow.com/questions/475888/will-this-lead-to-a-memory-leak-in-c">discussion</a> I read some stuff about smart pointers, they look great but I can't still figure out how to solve my problem with them. They deal with memory freeing and pointer copying, but I'm basicly using references, so they don't look the right choice for me. But I may be wrong.</p> <p>Maybe the only solution is to pass by value, maybe I can't get both efficiency and a good interface. But again, you're the expert, and I would like to know your opinion.</p>
 

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