开发者

Memory management differences in return by value

I'm trying to follow a tutorial here: regarding overloading operators, and I've found something that's really confused me.

There was a previous question on this very website here where this tutorial was discussed, namely regarding how the variables in the class were preserved because the whole class was passed back by value.

Whilst experimenting with the开发者_如何学JAVA class definition I toyed with making the integer variables pointers (perhaps not sensible - but just to experiment!) as follows:

class CVector {  

  int* x;
  int* y;

  public:

  CVector () {};
  CVector (int,int);
  CVector operator + (CVector);
  ~CVector ();
};

In the class constructor I allocate memory for the two integers, and in the class deconstructor I delete the allocated memory.

I also tweak the overloaded operator function as follows:

CVector CVector::operator+ (CVector param) {
  CVector temp;
  *temp.x = *x + *param.x;
  *temp.y = *y + *param.y;
  return (temp);   
}

For the original code, where the class has simple integer variables the return by value of the entire class completes successfully.

However, after I change the variables to int pointers, the return by value of the class does not complete successfully as the integer variables are no longer intact.

I assume the deconstructor is being called when the temporary CVector goes out of scope and deletes these member integer pointers, but the class itself is still returned by value.

I'd like to be able to return by value the CVector with the memory allocated to its member variables intact, whilst ensuring the temporary CVector is correctly deleted when it goes out of scope.

Is there any way this can be done?

Many thanks!


The problem is that you are not following the rule of the three, which basically boils down to: *if you manage resources, then you should provide copy constructor, assignment operator and destructor for your class*.

Assuming that on construction you are allocating memory for the pointers, the problem is that the implicit copy constructor is shallow, and will copy the pointers, but you probably want a deep copy. In the few cases where you do not want a deep copy, control of the manage shared resource becomes more complicated, and I would use a shared_ptr rather than trying to do it manually.


You need to provide a copy constructor for CVector to make copies of the allocated memory. Otherwise, when you return by value, the pointer values will simply be copied and then the temp object is destructed, deallocating the ints. The returned copy now points to invalid memory.

CVector( const CVector& other )
: x ( new int(other.x) )
, y ( new int(other.y) )
{}

Note that it is bad idea to be using raw pointers in your class, especially more than one. If the allocation of y fails above and new throws you've got a memory leak (because x is left dangling). You could've allocated within the constructor itself, instead of the initializer list, either within a try-catch, or using the std::nothrow version of new, then check for nullptr. But it makes the code very verbose and error prone.

The best solution is to use some smart pointer class such as std::unique_ptr to hold pointers. If you were to use std::shared_ptr to hold those pointers, you can even share the ints between copies of the class.


The return-by-value causes the returned, temp object to be copied to another object, a temporary "return object". After temp is copied, it is destructed, deallocating your ints. The easiest way to handle this is to use a reference-counted pointer such as tr1::shared_ptr<>. It will keep the memory allocated until the last reference to it is dropped, then it will deallocate.


There are few problems in the given code.

(1) You should allocate proper memory to *x and *y inside the constructor; otherwise accessing them is undefined behavior.

CVector () : x(new int), y(new int) {}

Also make sure that to have copy constructor and operator = where you delete x and delete y before reallocating them; otherwise it will lead to hazards.

(2) delete them in destructor

~CVector () { delete x; delete y; }

(3) Pass argument to operator + by const reference to avoid unnecessary copying.

CVector CVector::operator+ (const CVector &param)
{
  // code
}

Since you are learning with playing around with pointers, I will not comment on the design perspective of your class, like if they should be pointer or variables or containers and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜