Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Memory leak pro...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 43052 of 47545
Post > Topic >>

Memory leak problem?

by Philip Ogunbona <stingrayon@[EMAIL PROTECTED] > Feb 2, 2008 at 09:45 AM

This code is an implementation of class member function. The whole code 
where the class and the member function is being used compiles and works 
as expected. The problem is that I am returning a pointer to a container 
     I created in this member function and I have not DELETED it. Does 
this not constitute a memory leak? How do I get around it without 
changing the signature of the member function?

Regards,

Philip O.

Code implementation of member function.
=======================================

vector<int> * kNN::classify(vector<vector<double> > &testvector)
{

	vector<int> *classVector = new vector<int>(testvector.size());
		
	vector<pair<int,double> > distance(numFeatures); // numFeatures is a 
data member of class kNN
	
	for (int i =0; i < testvector.size(); i++)
	{
		for (int j = 0; j < numFeatures; j++)
		{	
// pick up the class of the feature; it is the last element of the vector
			distance[j].first = int(trainData[j][vecLen-1]);
			//compute L2 distance between probe/testvector and each of the 
training vector
			distance[j].second = euclidean(testvector[i], trainData[j]);
		}
		//sort the distance vector in ascending order of magnitude
		stable_sort(distance.begin(), distance.end(), lessDistance());
		//pick the class with the most appearance in the top k entries
		(*classVector)[i]=k_vote(distance);
		
	}
	#ifdef DEBUG
	copy((*classVector).begin(), (*classVector).end(), 
std::ostream_iterator<int>(cout, " "));
	cout  << endl;
	#endif
	return classVector;
		
}
 




 5 Posts in Topic:
Memory leak problem?
Philip Ogunbona <sting  2008-02-02 09:45:22 
Re: Memory leak problem?
"Bo Persson" &l  2008-02-02 13:07:21 
Re: Memory leak problem?
Stingray <stingrayon@[  2008-02-02 12:56:17 
Re: Memory leak problem?
jkherciueh@[EMAIL PROTECT  2008-02-02 07:50:09 
Re: Memory leak problem?
Stingray <stingrayon@[  2008-02-02 13:21:25 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Sep 4 23:53:12 CDT 2008.