Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Summary: Objects allocated with <code>new</code> must be deleted with <code>delete</code>. Objects allocated with <code>new[]</code> must be deleted with <code>delete[]</code>. Globals and locals are deleted automatically when their scope/TU execution ends. In <code>Binary&amp; operator +(Binary&amp; a)</code> you make a <code>Binary</code> that is leaked, and in <code>main</code> you make another <code>Binary</code> that is leaked. </p> <p>These problems would be avoided if wrote <code>operator+</code> like so:</p> <pre><code>Binary operator +(Binary&amp; a) const{ //return by value Binary b1(*this); //hold it on the stack //math here return b1; //return by value } </code></pre> <p>and if in main you avoided allocation as well:</p> <pre><code>Binary b2 = b+x; x = b2; b2.print(); </code></pre> <p>This will be faster than your original code, is easier to read and understand, and won't leak.</p> <p>[Other notes] </p> <p>Use a <code>std::vector</code> for the internal data instead of managing your own dynamic array. <code>vector</code> is easier, and less likely to make mistakes. </p> <p>It's usually best to make conversions (like <code>int</code> -> <code>Binary</code>) explicit wherever you can. It adds typing, but saves headaches. This goes for your <code>int</code> conversion operator as well. </p> <p>Make your const functions const. Right now, if you get a <code>const Binary</code>, you can't do <em>almost anything</em> with it. You can't print it, you can't add anything to it... </p> <p>You appear to be storing one bit per <code>int</code>, which means you're using about 97% more space than needed (wasting 99.99999995% of the values), which is just silly. Most novices start with <code>0-9</code> per <code>char</code>, which only wastes 50% of the space. (though that's still 96% of the values), but is really easy to understand.</p> <p>The normal way to do addition is like this:</p> <pre><code>Binary&amp; operator+=(const Binary&amp; rhs) { int max = a.first1&gt;this-&gt;first1? a.first1 : this-&gt;first1,bit; for (int i=0; i&lt;=max ; i++) { bit=a.digits[i]+this-&gt;digits[i]+overflow; overflow=0; if (bit&gt;1) overflow=1; b1-&gt;digits[i]=bit%2; } } Binary friend operator+(Binary lhs, const Binary&amp; rhs) { {return lhs+=rhs;} </code></pre>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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