So I was learning OOP in C++ and I thought it would be a good practice to write my own string class (for learning purposes, of course). I came up with a problem which I didn't know how to solve. Here's some peace of code:
class String {
char *str;
public:
String(char const *str);
~String();
String operator + (char const *str);
};
String::String(char *str) {
this->str = _strdup(str);
}
String::~String() {
free(this->str);
}
String String::operator+(char const *str) {
char *temp = (char *) malloc(strlen(str) + strlen(this->str) + 1);
strcpy(temp, this->str);
strcat(temp, str);
return temp;
}
The problem here is, that this piece of code will cause a memory leak. Return from "operator +" invokes my constructor, which copies temp by allocating more memory and I couldn't find any way how can I free it.
Your operator +
is defined as returning a String
but you're returning a char*
which means the compiler is implicitly converting it using the constructor. This copies the string but doesn't free the original which you are therefore leaking.
There are lots of things you could do to improve the code, as others have suggested, but to fix the actual leak you could do this:
String String::operator+(char const *str) {
char *temp = (char *) malloc(strlen(str) + strlen(this->str) + 1);
strcpy(temp, this->str);
strcat(temp, str);
String strTmp(temp);
free(temp);
return strTmp;
}
Writing a string class is not a simple task, if you want to do it right. For the particular problem that you are facing I can make a couple of recommandations...
Implement append()
or operator+=
that creates a larger buffer copies the contents, swaps the internal buffer and the newly created and releases the old one.
Then operator+
becomes a trivial task:
String operator+(String lhs, String const & rhs) {
lhs += rhs; // lhs.append(rhs);
return lhs;
}
(Of course, this is assuming that you provide correct definitions of the copy constructor, and assignment operator)
You forgot to implement operator= and the copy constructor. If you don't provide your own, the compiler will implement them for you doing a member wise copy which causes your memory leak.