954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Non dereferencable iterator C++

Hi All,
I seem to be getting an error with my code when running Intel C++ compiler via Visual Studio 2005. The error generated is "Expression:map/set iterator not dereferencable"
I've given my code below in the hope that someone can spot the problem.

set<stl_index>::iterator  eit1, eit2=elmt_ids.begin();
    bool                               non_consecutive(false);
    
    // 0. checking whether the elements are consecutively numbered
    for ( eit2++, eit1=elmt_ids.begin(); eit1!=elmt_ids.end(); eit1++, eit2++ )
      if ( (*eit1)+1 != (*eit2) ) {
           non_consecutive=true;
           break;
tonylamb
Newbie Poster
4 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

The only problem I can see with your code is that eit2 will overflow, being one step in front of eit1 each time. I can't see anything which looks like it should give a compiler error. Could you paste a more complete example of your code, since the actual problem may be elsewhere.

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

Thanks for that Bench. Unfortunately I probably didn't say it clearly enough but my code compiles ok but the problem crops up during debugging.... Since you picked up the overflow of eit2 any ideas on how I can improve my code.....
Once again Thanks for any help you can offer......

tonylamb
Newbie Poster
4 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

In which case, the message may be telling you that you're attempting to de-reference the end() iterator (Which is actually 'one past the end'). To prevent the overflow problem, you might consider changing your loop terminating condition to
eit2!=elmt_ids.end()

If eit1 is always one step behind eit2, then this check ought to ensure both iterators stay within the boundaries ofelmt_ids begin/end range.

edit - fixed BB code tag

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

Thanks Bench it worked....I know I'm being a pain now but I new error surfaces during debug "Expression: deque subscript out of range" and the debugger points to the following bit of code... stl_index here is type unsignedint32..... I think alot of my problems are cropping up due to my novice status to OOC++

stl_index                  nodes_per_element = plist[0].size();
    deque<vector<stl_index> >::iterator  it;
tonylamb
Newbie Poster
4 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

it would appear that your plist is empty at the point where your debugger has reached that portion of code.

You might prefer to use the deque::at() function, which is bounds checked, and throws an out_of_range exception if you attempt to retrieve something from the deque which doesn't exist.

alternatively, you could check the element you're accessing against the size of plist

if( 0 < plist.size() )
{
    stl_index nodes_per_element = plist[0].size();
    deque<vector<stl_index> >::iterator it;
    //etc.
Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

Thanks again Bench....Sorry it took me a few days to respond. My PhD supervisor uses all the code files that I'm using but on Metrowerks Code Warrior and does not seem to have any issues. He believes I'm having problems becuse the Intel Compiler uses all the microsoft headers...... I'd welcome any comments or suggestions that you might have......

tonylamb
Newbie Poster
4 posts since Feb 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You