1

I'm having trouble figuring out a way to return an object (declared locally within the function) which has dynamic memory attached to it. The problem is the destructor, which runs and deletes the dynamic memory when the object goes out of scope, i.e. when I return it and want to use the data in the memory that has been deleted! I'm doing this for an overloaded addition operator.

I'm trying to do something like:

MyObj operator+( const MyObj& x, const MyObj& y )
{
   MyObj z;

   // code to add x and y and store in dynamic memory of z

   return z;
}

My destructor is simply:

MyObj::~MyObj()
{ delete [] ptr; }

Any suggestions would be much appreciated!

Victor Brunell
  • 5,668
  • 10
  • 30
  • 46
  • 3
    Read This: [`C++: The Rule of Three`](http://en.wikipedia.org/wiki/Rule_of_three_(C++_programming)), and then [this question and answers](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – WhozCraig Nov 04 '13 at 21:52

2 Answers2

3

It's OK.

Don't worry, before deletion, your object will be copied into another object or will be used temporary.

But...

Try to write a well defined copy-constructor (if you don't have it). You should obey rule of five.

On the other hand, your code has a good chances for RVO optimization to avoid unnecessary copies and one extra destructing.

Moreover, C++11 presents move semantics to avoid unnecessary copies. To have this, you should write move-constructor and move-assignment.

Community
  • 1
  • 1
masoud
  • 55,379
  • 16
  • 141
  • 208
  • You may be right about RVO, but relying on that is dangerous. – Dima Nov 04 '13 at 21:55
  • Indeed, the statement that I wrote is targeting optimization, not disobeying the rules. – masoud Nov 04 '13 at 21:56
  • Wait, it is not OK if `MyObj` does not have a copy-constructor that copies the contents of `ptr`. – Dima Nov 05 '13 at 16:13
  • @Dima: Read the statement about "rule of five" in my answer -- Since we don't aware that he has a well copy-constructor defined, we can assume he _has_ a well defined copy-constructor. On the other hand, I mentioned that he needs it. The text is clear enough. – masoud Nov 05 '13 at 16:15
  • What I mean is that your explanation is ambiguous. You say "It's Ok" in big bold letters, which can lead the reader to believe that the code is fine as is. I would say "It is Ok, as long as you obey the rule of five" at the top, to make it absolutely clear. – Dima Nov 05 '13 at 16:18
  • @Dima: Reading the question carefully, he's worry about destruction of the object and this answer is talking about that. – masoud Nov 05 '13 at 16:20
  • Exactly. If `MyObj` has a copy-constructor, then it's ok. But if not, this is a bug. And we do not know whether or not `MyObj` has a copy-constructor. – Dima Nov 05 '13 at 16:29
  • Thanks for the replies. I did have a copy constructor and a destructor declared. The problem turned out to be an issue in the basic constructor. It wasn't able to implement the object I wanted to create in the operator overload. I fixed it and it runs smoothly now. Thanks for the insights into the copy constructor and when it runs. That helped narrow it down quite a bit. – Victor Brunell Nov 07 '13 at 16:12
2

You need to provide a copy constructor that copies the contents of ptr to the new object.

If MyObj does not have a copy constructor that copies the contents of ptr then you returned object will have its ptr point to deleted memory. Needless to say, if you try to access ptr in this situation, then bad things will happen.

Generally, if you had to write a destructor for your class, you should also write a copy constructor and the assignment operator to handle the copying of any dynamic memory or other resources. This is the Rule of Three mentioned by WhosCraig.

If you are using a modern compiler that supports C++11, you may also want to read up on the move semantics

Community
  • 1
  • 1
Dima
  • 38,860
  • 14
  • 75
  • 115