473,383 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

List Iterator Not Incrementable?

Hi,

I think this might be a VC++ problem. I am using Microsoft Visual
Studio 2005 Full Version (8.0).

I have a simple for loop that iterates over a list using the standard
iterator. The list is populated with a struct _BLOCK defined as:

typedef struct
{
int iX ;
int iY ;
int iFrame ;
double dIntensity ;
} _BLOCK ;

std::list<_BLOCK> list_block;
std::list<_BLOCK>::iterator list_block_it;

for (list_block_it = list_block.begin (); list_block_it !=
list_block.end (); list_block_it++)
{
list_block_it->dIntensity -= 0.025 ;
if (list_block_it->dIntensity < 0.0)
{ // Delete this item
list_block_it = list_block.erase (list_block_it);
}
}

The code seems fine to me, but it always fails an assert at run-time,
claiming "list iterator not incrementable." I haven't been able to
find any documentation or threads about this problem. Also, it seems
like this code runs fine when built with g++ or previous versions of
VC++.

Any help is greatly appreciated,

Nate

Jan 21 '06 #1
8 16922
<fr********@hotmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I have a simple for loop that iterates over a list using the standard
iterator. The list is populated with a struct _BLOCK defined as:

typedef struct
{
int iX ;
int iY ;
int iFrame ;
double dIntensity ;
} _BLOCK ;

std::list<_BLOCK> list_block;
std::list<_BLOCK>::iterator list_block_it;

for (list_block_it = list_block.begin (); list_block_it !=
list_block.end (); list_block_it++)
{
list_block_it->dIntensity -= 0.025 ;
if (list_block_it->dIntensity < 0.0)
{ // Delete this item
list_block_it = list_block.erase (list_block_it);
What if list_block_it becomes equal to list_block.end() at that point? It
could happen if erase erases the last item in the list.

Then, operator++ would be applied to an invalid iterator.
}
}

The code seems fine to me, but it always fails an assert at run-time,
claiming "list iterator not incrementable."
Your library is being very good to you :)
I haven't been able to
find any documentation or threads about this problem. Also, it seems
like this code runs fine when built with g++ or previous versions of
VC++.


Since it's undefined behavior, you are being unlucky on those platforms
because it "runs fine" on those platforms.

Ali

Jan 21 '06 #2
fr********@hotmail.com wrote in news:1137803247.499825.272320
@o13g2000cwo.googlegroups.com:
Hi,

I think this might be a VC++ problem. I am using Microsoft Visual
Studio 2005 Full Version (8.0).

I have a simple for loop that iterates over a list using the standard
iterator. The list is populated with a struct _BLOCK defined as:

typedef struct
{
int iX ;
int iY ;
int iFrame ;
double dIntensity ;
} _BLOCK ;

std::list<_BLOCK> list_block;
std::list<_BLOCK>::iterator list_block_it;

for (list_block_it = list_block.begin (); list_block_it !=
list_block.end (); list_block_it++)
{
list_block_it->dIntensity -= 0.025 ;
if (list_block_it->dIntensity < 0.0)
{ // Delete this item
list_block_it = list_block.erase (list_block_it);
And... kaboom (well, Undefined Behaviour). If you happen to be erasing
the last element in your list, you'll increment it again for the for
loop... thus you inadvertantly end up attemtping to iterate past the .end
() iterator. BTW: it's possibly that you're compiling with a "debug-
mode" standard library which is checking for things like attempting to
increment past the end of a container.
}
}

The code seems fine to me, but it always fails an assert at run-time,
claiming "list iterator not incrementable." I haven't been able to
find any documentation or threads about this problem. Also, it seems
like this code runs fine when built with g++ or previous versions of
VC++.

Jan 21 '06 #3
Ali,

Yes, it may be undefined behavior, but it did work before, whereas now
it does not. You were right about the source of the error. I modified
the code to fix it. Thanks for your help! Sometimes you just don't
notice the simple stuff.

Nate

Jan 21 '06 #4
fr********@hotmail.com wrote:
Hi,

I think this might be a VC++ problem. I am using Microsoft Visual
Studio 2005 Full Version (8.0).

I have a simple for loop that iterates over a list using the standard
iterator. The list is populated with a struct _BLOCK defined as:

typedef struct
{
int iX ;
int iY ;
int iFrame ;
double dIntensity ;
} _BLOCK ;

std::list<_BLOCK> list_block;
std::list<_BLOCK>::iterator list_block_it;

for (list_block_it = list_block.begin (); list_block_it !=
list_block.end (); list_block_it++)
{
list_block_it->dIntensity -= 0.025 ;
if (list_block_it->dIntensity < 0.0)
{ // Delete this item
list_block_it = list_block.erase (list_block_it);
}
}


Ali and Andre have already suggested the answer to your problem. I'd
like to address something else.

1. Your typedef is unnecessary, You should define it as "struct BLOCK"
(note the lack of an underscore), which brigns me to...
2. The identifier _BLOCK is reserved. Any identifier with a leading
underscore followed by a capital letter is reserved to the implementation.
Jan 21 '06 #5
red floyd,

I must admit that I did not write this code. I simply tried compiling
it for a friend under 2003, which worked, and 2005, which did not work.
I could not see a problem, when of course the problem was very
obvious. However, let's not pick it to pieces, shall we?

nate

Feb 2 '06 #6
fr********@hotmail.com wrote:
red floyd,

I must admit that I did not write this code. I simply tried compiling
it for a friend under 2003, which worked, and 2005, which did not work.
I could not see a problem, when of course the problem was very
obvious. However, let's not pick it to pieces, shall we?


It's sometimes difficult to accept criticism, and if you post code to
this group it WILL be criticised heavily, but then, that's what you're
asking for. Learn from the criticism, don't take it personally.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 2 '06 #7
fr********@hotmail.com wrote:
Hi,

I think this might be a VC++ problem. I am using Microsoft Visual
Studio 2005 Full Version (8.0).

I have a simple for loop that iterates over a list using the standard
iterator. The list is populated with a struct _BLOCK defined as:

typedef struct
{
int iX ;
int iY ;
int iFrame ;
double dIntensity ;
} _BLOCK ;

std::list<_BLOCK> list_block;
std::list<_BLOCK>::iterator list_block_it;

for (list_block_it = list_block.begin (); list_block_it !=
list_block.end (); list_block_it++)
{
list_block_it->dIntensity -= 0.025 ;
if (list_block_it->dIntensity < 0.0)
{ // Delete this item Rather than this obvious comment ...
.... I'd expect a rationale why the next element in the list is not
tested.
list_block_it = list_block.erase (list_block_it);
}
}

The code seems fine to me, but it always fails an assert at run-time,
claiming "list iterator not incrementable." I haven't been able to
find any documentation or threads about this problem. Also, it seems
like this code runs fine when built with g++ or previous versions of
VC++.

Any help is greatly appreciated,

Nate


The correct way to remove element from a sequence is:
typedef std::list<int> List; // just a short-hand
typedef List::iterator Iter; // just a short-hand

List list;
for (Iter i = list.begin(); i != list.end();) {
if (*i < 0) i = list.erase(i);
else ++i;
}

Regards, Stephan

Feb 2 '06 #8

fr********@hotmail.com wrote:
red floyd,

I must admit that I did not write this code. I simply tried compiling
it for a friend under 2003, which worked, and 2005, which did not work.
The VC2005 compiler is improved (duh). It catches many errors
immediately
whereas with the VC2003 compiler you would have other problems later
on.From experience, we prefer the VC2005 behavior. You can catch it before

the boss is looking ;)

Michiel.

Feb 2 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
4
by: Vince | last post by:
Hi, I am not used to template and I cannot find what's wrong with this code : Compiler(MSVS 7) complains at line : class iterator : public std::iterator<... Here is the source code
4
by: John | last post by:
I was wondering why the STL RB Tree implementation is used when the iterators of map are incremented/decremented to yield successors and predecessors? This takes O(log n) time (and a lot of memory...
6
by: Henrik Goldman | last post by:
Hello, I have a dataset which consist of a string username and string hostname as a key and then an integer representing a count as the matching "second" value in a pair. So far I've used...
1
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void...
1
by: sanjeevani | last post by:
Hi guyz, i am new to c++ programming..and this forum ...i just hope that i am giving you all the information which will enable you to figure out the problem. My program uses c++ and image...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
6
by: APEJMAN | last post by:
I know what I'm posting here is wired, but it's been 3 days I'm workin g on these codes, but I have no result I post the code here I dont wanne bother you, but if any one of you have time to...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.