Does this leak memory?

Started by
4 comments, last by Deyja 17 years, 7 months ago
I'm helping a friend with something, and he has this template class and I am wondering if it creates a memory leak, or if this would be cleaned up properly by the stack. If it does create a leak, what is a better solution? Fyi, this is for a Fixed point variable, where m_value = the data contained and shift is the number of bytes to the left the whole (integer part) of the variable would start.

//copy constructor
inline Fixed(const Fixed<T, shift>& fixed) : m_value(fixed.m_value) { };

//function
template <class T, int shift>
inline Fixed<T, shift>& Fixed<T, shift>::operator+(const Fixed<T, shift>& fixed)
{
	Fixed<T, shift> *result = new Fixed<T, shift>(*this);
	result->m_value = result->m_value + fixed.m_value;
	return *result;
}

Is derefercing that pointer in the overloaded operator + a bad idea? Initially he had the first line create a stack variable and then returned it, but he was concerned with speed, because it seemed to create another variable (or so he thought because the copy constructor would be called again during the return statement). Comments, questions concerns are welcome. thnx, tHom.
Advertisement
You're not storing the pointer anywhere (unless you're doing some evil with the returned reference) so it looks like a leak to me.

He was concerned about the perf hit of creating a stack temporary so he decided to call an allocator instead? The mind boggles. Or I'm really confused. From the sounds of it the original solution was probably much closer to optimal.
-Mike
Scott Meyers, "Effective C++," Item 23: Don't try to return a reference when you must return an object.

If you do not have Meyer's triptych and Sutter's "exceptional" books, and read them and reread them like a bible in a temperance hall, then you need to.

Stephen M. Webb
Professional Free Software Developer

Unless you can say where you delete what you new, you have a leak.

Where do you delete your Fixed<T, shift>(*this)?

You have a leak.

operator+ *should* typically return an object. Not by reference, not by pointer - a whole new object. Also, operator+ should be done as a non-member friend using the binary version, so that (something_covertable_to_Fixed) + (Fixed) works. Your code refactored:

template < typename T , unsigned shift >class Fixed {    ... m_value;public:    Fixed( const Fixed< T , shift > & fixed ) : m_value(fixed.m_value) {}    //note: Implicitly inline since it is a member function defined within the class body    //note: No trailing semicolon after the closing braces of functions.    ...    Fixed< T , shift > & operator+=( const Fixed< T , shift > & fixed ) {        m_value += fixed.m_value;        return *this;    }    friend inline Fixed< T , shift > operator+( const Fixed< T , shift > & left , const Fixed< T , shift > & right ) {        Fixed< T , shift > result( left );        result += right;        return result;    }    //note: Might've been implicitly inline too. I froget.    ...};//note: We do need trailing semicolons after the closing braces of classes, though.
It is a leak. Tell your friend to put it back how he had it before. This is not faster, it is worse.

If you write it like below, your compiler may be able to take advantage of NRVO and should come out pretty fast.
template <class T, int shift>friend inline const Fixed<T, shift> Fixed<T, shift>::operator+(const Fixed<T, shift>& lhs, const Fixed<T, shift>& rhs){	Fixed<T, shift> result;	result.m_value = lhs.m_value + rhs.m_value;	return result;}
It's much simpler if you put all the function definitions inside the class definition in the header file though.

Edit: This was a cross-post, but you get the idea.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
You might also consider allowing the addition of Fixeds with different Shifts.

This topic is closed to new replies.

Advertisement