User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,854 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,386 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 892 | Replies: 6
Reply
Join Date: Feb 2008
Posts: 4
Reputation: tonylamb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tonylamb tonylamb is offline Offline
Newbie Poster

Help Non dereferencable iterator C++

  #1  
Feb 9th, 2008
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.

  1.  
  2. set<stl_index>::iterator eit1, eit2=elmt_ids.begin();
  3. bool non_consecutive(false);
  4.  
  5. // 0. checking whether the elements are consecutively numbered
  6. for ( eit2++, eit1=elmt_ids.begin(); eit1!=elmt_ids.end(); eit1++, eit2++ )
  7. if ( (*eit1)+1 != (*eit2) ) {
  8. non_consecutive=true;
  9. break;
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Non dereferencable iterator C++

  #2  
Feb 9th, 2008
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.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Feb 2008
Posts: 4
Reputation: tonylamb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tonylamb tonylamb is offline Offline
Newbie Poster

Re: Non dereferencable iterator C++

  #3  
Feb 9th, 2008
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......
Last edited by tonylamb : Feb 9th, 2008 at 7:27 am.
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Non dereferencable iterator C++

  #4  
Feb 9th, 2008
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 of elmt_ids begin/end range.



edit - fixed BB code tag
Last edited by Bench : Feb 9th, 2008 at 7:34 am.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Feb 2008
Posts: 4
Reputation: tonylamb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tonylamb tonylamb is offline Offline
Newbie Poster

Re: Non dereferencable iterator C++

  #5  
Feb 9th, 2008
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++
  1. stl_index nodes_per_element = plist[0].size();
  2. deque<vector<stl_index> >::iterator it;
Last edited by tonylamb : Feb 9th, 2008 at 8:09 am.
Reply With Quote  
Join Date: Feb 2006
Location: UK
Posts: 468
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Rep Power: 5
Solved Threads: 42
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: Non dereferencable iterator C++

  #6  
Feb 9th, 2008
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
  1. if( 0 < plist.size() )
  2. {
  3. stl_index nodes_per_element = plist[0].size();
  4. deque<vector<stl_index> >::iterator it;
  5. //etc.
¿umop apisdn upside down?
Reply With Quote  
Join Date: Feb 2008
Posts: 4
Reputation: tonylamb is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
tonylamb tonylamb is offline Offline
Newbie Poster

Re: Non dereferencable iterator C++

  #7  
Feb 12th, 2008
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......
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Other Threads in the C++ Forum

All times are GMT -4. The time now is 8:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC