Register for your free account! | Forgot your password?

Go Back   elitepvpers > Coders Den > C/C++
You last visited: Today at 13:36

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



[C++] Class with char * member

Discussion on [C++] Class with char * member within the C/C++ forum part of the Coders Den category.

Reply
 
Old   #1
 
elite*gold: 115
Join Date: Oct 2007
Posts: 9,390
Received Thanks: 12,345
[C++] Class with char * member

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
ms​ is offline  
Old 07/31/2010, 11:04   #2
 
Shadow992's Avatar
 
elite*gold: 77
Join Date: May 2008
Posts: 5,430
Received Thanks: 5,878
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;

Shadow992 is offline  
Old 07/31/2010, 12:58   #3
 
elite*gold: 115
Join Date: Oct 2007
Posts: 9,390
Received Thanks: 12,345
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;
}
ms​ is offline  
Old 08/01/2010, 15:23   #4
 
elite*gold: 20
Join Date: Sep 2006
Posts: 1,100
Received Thanks: 184

kwt
Bot_interesierter is offline  
Old 08/01/2010, 17:28   #5


 
MrSm!th's Avatar
 
elite*gold: 7110
Join Date: Jun 2009
Posts: 28,903
Received Thanks: 25,407
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[]!
MrSm!th is offline  
Old 08/03/2010, 16:58   #6
 
elite*gold: 577
Join Date: Oct 2009
Posts: 665
Received Thanks: 3,502
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
tim66613 is offline  
Thanks
1 User
Reply


Similar Threads Similar Threads
What is the best class for an euro char?
05/25/2011 - Silkroad Online - 34 Replies
Hello! Im new in Silkroad and i wanted to know which class would be the best for an european because i want to make a euro char, but im not sure which class to choose...:confused: Please guys, give me the name of a classe you enjoyed playing with or a class that is good to begin with.
ARCHON WTB 70+ char class doesnt mather
08/17/2010 - Archlord Trading - 0 Replies
yeah so im looking for a char 70 + rather 80 + with some realm if you have if not its okei too with email .. thats a must.. :) make me happy
wtt a lvl 90 last chaos char ( wizzard ) for a silkroad lvl 60+ any class
04/08/2010 - Last Chaos Trading - 1 Replies
hello guys im waane trade my last chaos acc for a silkroad acc full eva set +6-+8 and +15 wapon have horse lvl 37 drake lvl 35 pink ! a guild lvl 9 and item mall stuff let me now something a lvl 94 cleric +15 want and lvl 31 pet trainer whit mount 31 and pet lvl 23 al on the same acc
how much dil for a 115+ naked char any class in 2moons?
02/18/2009 - Dekaron Trading - 0 Replies
just wondering, if nay1 has an answer to the question in title
What is the best class for an euro char?
01/06/2008 - Silkroad Online - 13 Replies
Hello! Im new in Silkroad and i wanted to know which class would be the best for an european because i want to make a euro char, but im not sure which class to choose...:confused: Please guys, give me the name of a classe you enjoyed playing with or a class that is good to begin with.



All times are GMT +1. The time now is 13:37.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.