Where's the destructor of ustring
? Shouldn't it free the allocated memory?
Let's look e.g. at the assignment operator:
ustring operator=(ustring input)
{
ustring result(input);
...
return *this;
}
You pass a ustring
parameter by value. This will likely result in a copy being created via the copy constructor. You invoke copy construction another time to initialize result
. I suspect that you invoke copy construction yet another time when you return *this
as a ustring
(again by value, instead of by reference).
Let's look at one of these three cases, namely result
: When this local variable goes out of scope (ie. at the end of the function), it will be automatically destroyed. But if you don't provide a destructor (~ustring
) that free
s the allocated memory, you'll get a memory leak.
And since you apparently have lots of copy construction and pass-by-value going on without having a destructor for freeing the allocated memory, you'll get lots and lots and lots of unfreed memory.
Besides: Why do you use malloc
and free
instead of new[]
and delete[]
? You could get rid of the ugly sizeof(int) * ...
calculations and (int*)
type casts. Instead of:
values = (int *) malloc(sizeof(int) * len);
you'd simply write:
values = new int[len];