1

Here is an example of a class that is made available for the + operation.

class A
{
public:
   int *array;

   A()
   {
      array = new int[10];
   }

   ~A()
   {
      delete[] array;
   }

   A operator+ (const A &b)
   {
      A c;
      for(int i=0; i<10; i++)
         c.array[i] += array[i] + b.array[i];
      return c;
   }
};

int main()
{
   A a,b,c,d;

   /* puts some random numbers into the arrays of b,c and d */
   a = b+c+d;
}

Will a run the destructor before copying the result of b+c+d or not? If not, how do I make sure no memory is leaked?

The + operator overload is designed this way such that no operand is modified.

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Kang Min Yoo
  • 805
  • 1
  • 9
  • 20
  • What was wrong with the accepted answer to [your previous question](http://stackoverflow.com/q/10360232/78845)? And why are you using `new int[10]` rather than `std::vector`? – johnsyweb Apr 28 '12 at 08:43
  • I'm assuming this is just an example to try to understand, but if this were real code, you could just define `int array[10];` and then not have to worry about proper deletion. – Corbin Apr 28 '12 at 08:45
  • @Johnsyweb The answer was great, but this was just an example that I sketched without much thinking. This example is no way indicative of my view towards other answers. – Kang Min Yoo Apr 28 '12 at 08:52
  • @Corbin In the code I'm working on right now, the constructor receives a set of integers, which will be used to allocate a certain size of arrays. – Kang Min Yoo Apr 28 '12 at 08:54
  • Ah. If you're already working with standard containers, then Johnsyweb is right; you should likely just use a vector. – Corbin Apr 28 '12 at 08:55
  • Hmmm... What are you actually trying to achieve? This looks like a job for `std::vector`, `std::transform()` and `std::plus`. – johnsyweb Apr 28 '12 at 09:04

2 Answers2

9

You need to add an equals operator to A. Also, you will likely want to create a copy constructor.

When a becomes the return from b+c+d, the array pointer in a gets over written without delete[] ever being called on it. You need to make an operator= that deletes the array.

An example of an operator= is below:

A& operator=(A const& a)
{
    if (&a != this) {
        int* tmp = this->array;
        this->array = new int[10];
        //copy a.array to this->array
        delete[] tmp;
    }
    return *this;
}

There's a lot of subtleties in this if you're new to operator=.

In particular, the check whether or not a is equal to this is necessary because it's perfectly valid to write:

A a;
a = a;

This would cause a pointless copy, and in most cases of operator= will cause bugs.

The other subtlety is less of a requirement than that one and more of a coding style (though a very wide spread standard). When copying something that is dynamically allocated, you always want to allocate and copy before you release. That way, if new throws an exception (or something else fails), the object is still in a stable state, though with it's old data instead of the new expected dated.

Corbin
  • 33,060
  • 6
  • 68
  • 78
2

Will this cause memory leak?

Yes, it will. You forgot to add copy constructor and assignment operator. See Rule of Three

You could also use std::vector<int> for A::array instead of int*. In this case you wouldn't need to worry about copy constructor/assignment operator (as long as you don't add something else that must be handled in destrcutor).

SigTerm
  • 26,089
  • 6
  • 66
  • 115