[C++] Class with char * member

07/31/2010 01:01 ms​#1
So I've written the following class:
Code:
#define BUFLEN 100000

class myClass
{
	public:
		myClass();
		~myClass();

	private:
		char *buf;
};

myClass::myClass()
{
	buf = new char[BUFLEN];
}

myClass::~myClass()
{
}
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:

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;
}
This code runs, but after some time it throws a memory exception and I have no idea why.


Need help! ;O
07/31/2010 11:04 Shadow992#2
Quote:
Originally Posted by Disconnect View Post
So I've written the following class:
Code:
#define BUFLEN 100000

class myClass
{
	public:
		myClass();
		~myClass();

	private:
		char *buf;
};

myClass::myClass()
{
	buf = new char[BUFLEN];
}

myClass::~myClass()
{
}
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:

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;
}
This code runs, but after some time it throws a memory exception and I have no idea why.


Need help! ;O
Hmm, i think the problem is that buf is an array (or can be one) and you are only deleting the first index, try this:
PHP 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->bufrmyClass.bufBUFLEN);
}

myClass::~myClass()
{
    
delete[] buf;

07/31/2010 12:58 ms​#3
delete and delete[] appear to have the same result. However, I think I solved the problem.

Code:
#define BUFLEN 100000

class myClass
{
	public:
		myClass();
		myClass(const myClass &rmyClass);
		~myClass();
		myClass &myClass::operator=(const myClass &rmyClass)

	private:
		char *buf;
};

myClass::myClass()
{
	buf = new char[BUFLEN];
}

myClass::myClass(const myClass &rmyClass)
{
	buf = new char[BUFLEN];
	*this = myClass;
}

myClass::~myClass()
{
	delete buf;
}

myClass &myClass::operator=(const myClass &rmyClass)
{
	if (this != &rmyClass)
		memcpy(buf, rmyClass.buf, BUFLEN);
	return *this;
}
08/01/2010 15:23 Bot_interesierter#4
[Only registered and activated users can see links. Click Here To Register...]
kwt
08/01/2010 17:28 MrSm!th#5
Quote:
Originally Posted by Disconnect View Post
delete and delete[] appear to have the same result. However, I think I solved the problem.

Code:
#define BUFLEN 100000

class myClass
{
	public:
		myClass();
		myClass(const myClass &rmyClass);
		~myClass();
		myClass &myClass::operator=(const myClass &rmyClass)

	private:
		char *buf;
};

myClass::myClass()
{
	buf = new char[BUFLEN];
}

myClass::myClass(const myClass &rmyClass)
{
	buf = new char[BUFLEN];
	*this = myClass;
}

myClass::~myClass()
{
	delete buf;
}

myClass &myClass::operator=(const myClass &rmyClass)
{
	if (this != &rmyClass)
		memcpy(buf, rmyClass.buf, BUFLEN);
	return *this;
}
It should not.
delete is for single elements, but if you allocate more than one element you should use delete[]!
08/03/2010 16:58 tim66613#6
I think reference counting is the best way to solve your problem:

Code:
#define BUFLEN 100000

class myClass
{
	public:
		myClass();
		myClass(const myClass& rmyClass);
		~myClass();
		myClass& operator=(const myClass& rmyClass);


	private:
		struct _Holder
		{
			_Holder(char* p)
				: m_realPtr(p)
				, m_refCount(1)
			{
				// ctor
			}

			~_Holder()
			{
				delete[] m_realPtr;
			}

			char* m_realPtr;
			unsigned int /*uint32_t*/ m_refCount;
		};

		_Holder* m_data;
};

myClass::myClass()
{
	m_data = new _Holder(new char[BUFLEN]);
}

myClass::myClass(const myClass& rmyClass)
	: m_data(rmyClass.m_data)
{
	m_data->m_refCount++;
}

myClass::~myClass()
{
	if(--m_data->m_refCount == 0)
		delete m_data;
}

myClass& myClass::operator=(const myClass& right)
{
	right.m_data->m_refCount++;

	if(--m_data->m_refCount == 0)
		delete m_data;

	m_data = right.m_data;
}
Kind Regards Tim