Testing for end()

T

Tricky

Using std::vector, I am seeing the assertion:

"vector iterator not dereferencable"

I know exactly where the problem is but am not sure how to work around it.
The error occurs in a function like this:

(This is not the actual code that I'm posting, which I know is discouraged,
but it would just be too much to ask everyone to wade through... It's a
fairly complex text parser with lots of recursion, etc.)

----------

bool myclass_1::match(std::vector<myclass_2 *>::iterator &pos)
{
if ((*pos)->x == x) // x is just some member of both myclass_1 and 2
{
pos++;
return true;
}

return false;
}

----------

The assertion fails because it's possible that "pos" will be equal to
vector::end() by the time it reaches this function. Logically, that is
acceptable, and the function should just return false in that case.

So, the question is, how can I check "pos" to see if it is invalid before
trying to dereference it?

I guess that I could pass a reference to the vector from which pos was
created into the "match" function... But there are many, many of these
match functions and the vector itself just isn't used in them. So it seems
like unnecessary overhead just to provide access to the "end()" function.
Is that the only solution?
 
V

Victor Bazarov

Tricky said:
Using std::vector, I am seeing the assertion:

"vector iterator not dereferencable"

I know exactly where the problem is but am not sure how to work around it.
The error occurs in a function like this:

(This is not the actual code that I'm posting, which I know is discouraged,
but it would just be too much to ask everyone to wade through... It's a
fairly complex text parser with lots of recursion, etc.)

----------

bool myclass_1::match(std::vector<myclass_2 *>::iterator &pos)
{
if ((*pos)->x == x) // x is just some member of both myclass_1 and 2
{
pos++;
return true;
}

return false;
}

----------

The assertion fails because it's possible that "pos" will be equal to
vector::end() by the time it reaches this function. Logically, that is
acceptable, and the function should just return false in that case.

So, the question is, how can I check "pos" to see if it is invalid before
trying to dereference it?

I guess that I could pass a reference to the vector from which pos was
created into the "match" function... But there are many, many of these
match functions and the vector itself just isn't used in them. So it seems
like unnecessary overhead just to provide access to the "end()" function.
Is that the only solution?

Nope. The idiomatic solution is to pass the actual iterator [that
'end()' returns] to be compared with.

Iterators [should] know nothing about the containers they iterate. Nor
should your algorithm if it is built with iterators as its interface.
But if you need some special action in case the iterator has some
special value, pass that value to your algorithm.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top