So I've written the following class:
This code is being executed as i want, but obviously it contains a memory leak. Simply deleting the allocated memory in the destructor doesn't help since I want to use this class in a vector. When an object is copied and then deleted the char * of the copy points to an invalid memory address.
After using the copy constructor as well I ended up with this code:
This code runs, but after some time it throws a memory exception and I have no idea why.
Need help! ;O
Code:
#define BUFLEN 100000
class myClass
{
public:
myClass();
~myClass();
private:
char *buf;
};
myClass::myClass()
{
buf = new char[BUFLEN];
}
myClass::~myClass()
{
}
After using the copy constructor as well I ended up with this code:
Code:
#define BUFLEN 100000
class myClass
{
public:
myClass();
myClass(const myClass &rmyClass);
~myClass();
private:
char *buf;
};
myClass::myClass()
{
buf = new char[BUFLEN];
}
myClass::myClass(const myClass &rmyClass)
{
*this = myClass;
this->buf = new char[BUFLEN];
memcpy(this->buf, rmyClass.buf, BUFLEN);
}
myClass::~myClass()
{
delete buf;
}
Need help! ;O