Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


Passing iterator in a function call


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 MrRage   Members   -  Reputation: 124

Like
0Likes
Like

Posted 22 March 2007 - 07:59 PM

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 ¤t; [Edited by - MrRage on March 24, 2007 1:20:08 AM]

Sponsor:

#2 ToohrVyk   Members   -  Reputation: 1591

Like
0Likes
Like

Posted 22 March 2007 - 08:06 PM

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.

#3 _goat   Members   -  Reputation: 804

Like
0Likes
Like

Posted 22 March 2007 - 08:08 PM

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.

[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS