• 0

Values of the function parameters are changing randomly (but they're not modified from the code)


Question

Hi, I have to implement an NBC (for finding clusters in the provided set of data) algorithm at my class project with a friend. We came across very strange issue. There are few helper functions, and the one with a problem is kNN (possibly kEN too) in the kNB.h file. After passing arguments to it from the main function of the program (for example k=3 and p=5), it goes to the kNN function and starts changing values of k and p randomly, even though function code is not supposed to do that anywhere as you can see below.

 

Also, while using debugging mode and going through this function step by step I noticed that it sometimes comes back before the first while which I think shouldn't happen. I guess it may be some trivial mistake, but I'm not very good at C++ (unfortunately we were ordered to use it). You can download entire Visual Studio 2013 solution from here: https://dl.dropboxusercontent.com/u/1561186/EDAMI.rar. So, does anyone have any idea why described situation is happening?

static vector<int> kNN(int k, int p, Dataset<V>* records)
{
	int b = p, f = p;
	bool backwardSearch, forwardSearch;
	vector<int> tmp;
	LengthMetric<V>* lengthMetric = records->getLengthMetric();

	backwardSearch = PrecedingPoint(records, b);

	forwardSearch = FollowingPoint(records, f);

	int i = 0;

	while (backwardSearch && forwardSearch && i < k)
	{

		if (records->getRecord(p)->getLength() - records->getRecord(b)->getLength() < records->getRecord(f)->getLength() - records->getRecord(p)->getLength())
		{
			i++;
			tmp.push_back(b);
			backwardSearch = PrecedingPoint(records, b);
		}
		else
		{
			i++;
			tmp.push_back(f);
			forwardSearch = FollowingPoint(records, f);
		}
	}

	while (backwardSearch && i < k)
	{

		i++;
		tmp.push_back(b);
		backwardSearch = PrecedingPoint(records, b);
	}

	while (forwardSearch && i < k)
	{
		i++;
		tmp.push_back(f);
		forwardSearch = FollowingPoint(records, f);
	}

	return tmp;
}

5 answers to this question

Recommended Posts

  • 0

its because you're debugging the release executable. turn optimizations off when you want to debug. configuration properties->c/c++->optimization set to disable. or just set the configuration type to debug instead of release when you want to debug. in your project,the active configuration currently is release,not debug.

3yA2sxi.png

  • 0

I ran your code and cannot observe the behavior you describe. The code cannot modify the values of k and p unless perhaps through stack corruption. How did you determine that the values change?

This topic is now closed to further replies.