Skip to main content
GameDev.net gamedev.net
🔒 Locked

Passing iterator in a function call

Started by MrRage Mar 23, 2007 at 1:59 AM 1 replies 2k views
Original Post
MrRage
MrRage
Edited: My apologies, I was tied when I posted this and didn’t do a good job explaining my problem. When I past a pre-defined iterator into my parser function and try to compare it against one I created inside that parser function, the function would fail. But when I just past an offset value and created both iterators inside the function – it worked fine. So I’m not sure if I past the iterator correctly or not. I have another function that takes an iterator as a peramiter and that works fine, but it doesn’t compare the iterator to another iterator. So am I missing something about copies of iterators, or is this suppose to work and maybe I just did something dump in the original code and didn’t duplicate it in the example given? I just modified the code I had back to the way it was when it was failing, but didn't svn the failing code before modifying it =( Example:

typedef std::vector<char> ByteVector;
typedef std::vector<std::string> ResultSet;

ResultSet Parser::split(ByteList msg, ByteList::iterator current) {
	/////////////////////////////////////////
	// Configure the ResultSet
	ResultSet results;
	ByteList::iterator end = msg.end();
	ByteList::iterator next = current;
	end -= 4;	// lets skip over the '001*'
	next++;		// Because we want to see whats comming next with out moving
	
	std::string item;
	while (current != end) {
		if (((*current) == ' ' || (*current) == ':' ) || next == end) {
			if (item.length() > 0) {
				string copy = item;
				results.push_back(copy);
				item = "";
			}
		} else {
			item.push_back((*current));
		}
		current++;
		next++;
	}
	return results;
}

...

ByteVector msg = someFunc();  // returns a 92 char long vector
ByteVector::iterator start = msg.begin();
start += 51;
ResultSet rs = Parser::split(msg, start);

Edited: Fixed the problem by making ByteList::iterator &current [Edited by - MrRage on March 24, 2007 1:20:08 AM]
ToohrVyk
ToohrVyk
In your example code, you're trying to reach a iterator which is 40 elements before the end, in a vector which contains no elements at all. This is bound to cause problems.

If your example code is not representative of your actual code, please post the actual code between [source][/source] tags.
_goat
_goat
The end() function returns an iterator to one past the end of the list/array - that means the data it points to is always invalid. Performing the -= operator on the iterator will make it go backwards 40 iterations, which may end up being invalid too.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.