Hi,
I'm having problems allocating and deallocating my memory in a recursive C++ program. So without using an automatic memory management solution, I wonder if anyone can help me resolve the memory leak I am experiencing.
The following code essentially explains the problem (although it's a contrived example, please correct any mistakes or simplifications I've made).
A number class to hold the value of a number:
class Number
{
public:
Number() { value = 1; };
Number& operator + (const Number& n1) const
{
Number result = value + n1.value;
return result;
};
int value;
};
Two functions to perform the recursion:
Number& recurse(const Number& v1)
{
Number* result = new Number();
Number one = Number();
*result = *result + recurse(one);
return *result;
}
int main(...)
{
Number answer = Number();
answer = recurse(result);
}
As you can see the memory allocated in the recurse function is leaked, but I'm not sure where I can free up this memory from based on the nature of the recursion?
Thanks,
Dan