473,394 Members | 1,765 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,394 software developers and data experts.

need some help with list::end ()

Hi all,

I'm using stl's list to store some file names. Its declared as:

list < char * filenames;
i enumerate the list by using:

list < char * >::const_iterator filename;

filename = filenames.begin();

than I simply do a filename++ to go to the next item.....

I check for filenames.end() in an *if* condition somewhere in my code to
check if I'm dealing with the last item of the list. The code to do this was
working fine. But now due to some change in my code, some of the list items
are deleted *before* the filenames.end() check. I have noticed that this
check now never evaluates to true although I know in the debugger that I'm
standing at the last item. I'm sure I'm missing some basic concept here. How
can I check now that this is the last item of the list?

I'm using vc++ 2k5 total unmanaged.

Regards,

-ab.
Aug 15 '06 #1
10 1231

"Abubakar" <em**********@yahoo.comskrev i meddelandet
news:up**************@TK2MSFTNGP03.phx.gbl...
Hi all,

I'm using stl's list to store some file names. Its declared as:

list < char * filenames;
i enumerate the list by using:

list < char * >::const_iterator filename;

filename = filenames.begin();

than I simply do a filename++ to go to the next item.....

I check for filenames.end() in an *if* condition somewhere in my
code to
check if I'm dealing with the last item of the list. The code to do
this was
working fine. But now due to some change in my code, some of the
list items
are deleted *before* the filenames.end() check. I have noticed that
this
check now never evaluates to true although I know in the debugger
that I'm
standing at the last item. I'm sure I'm missing some basic concept
here. How
can I check now that this is the last item of the list?
How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.

You should be doing something like

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

This will *either* use the returned iterator to the next object, or
increment the current iterator. You should do one, but not both! :-)
Bo Persson
Aug 15 '06 #2
Sorry for the late reply.
How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.
I use the "remove" method.

I have the following declaration:

list<char * >::const_iterator name, prev;

at some point I do:

prev = name;
name ++;
filenames.remove ( *prev );

and this is how I remove the items.

regards,
-ab.
"Bo Persson" <bo*@gmb.dkwrote in message
news:4k************@individual.net...
>
"Abubakar" <em**********@yahoo.comskrev i meddelandet
news:up**************@TK2MSFTNGP03.phx.gbl...
Hi all,

I'm using stl's list to store some file names. Its declared as:

list < char * filenames;
i enumerate the list by using:

list < char * >::const_iterator filename;

filename = filenames.begin();

than I simply do a filename++ to go to the next item.....

I check for filenames.end() in an *if* condition somewhere in my
code to
check if I'm dealing with the last item of the list. The code to do
this was
working fine. But now due to some change in my code, some of the
list items
are deleted *before* the filenames.end() check. I have noticed that
this
check now never evaluates to true although I know in the debugger
that I'm
standing at the last item. I'm sure I'm missing some basic concept
here. How
can I check now that this is the last item of the list?

How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.

You should be doing something like

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

This will *either* use the returned iterator to the next object, or
increment the current iterator. You should do one, but not both! :-)
Bo Persson


Aug 16 '06 #3
Abubakar wrote:
Sorry for the late reply.
>How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.

I use the "remove" method.

I have the following declaration:

list<char * >::const_iterator name, prev;

at some point I do:

prev = name;
name ++;
filenames.remove ( *prev );

and this is how I remove the items.
If you have duplicates in your list, the call to remove will remove more
than one of them at a time, including possibly the one now referenced by
"name". Use the loop that Bo suggested:

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

that's the canonical way to iterate through a list removing some elements as
you go.

-cd
Aug 16 '06 #4
Carl Daniel [VC++ MVP] wrote:
Abubakar wrote:
>Sorry for the late reply.
>>How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.

I use the "remove" method.

I have the following declaration:

list<char * >::const_iterator name, prev;

at some point I do:

prev = name;
name ++;
filenames.remove ( *prev );

and this is how I remove the items.

If you have duplicates in your list, the call to remove will remove
more than one of them at a time, including possibly the one now
referenced by "name". Use the loop that Bo suggested:

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

that's the canonical way to iterate through a list removing some
elements as you go.
Also, how are you adding the items to the list? Since you're storing raw
char*'s, the list isn't managing the memory occupied by the strings - you
are. You should consider changing to a std::list<std::stringinstead.

-cd
Aug 16 '06 #5
char*'s, the list isn't managing the memory occupied by the strings - you
are.
list <char * filenames ;

WIN32_FIND_DATAA finddata;
//emailblockslocation
HANDLE filehandle = FindFirstFileA ( m_folderlocation , & finddata );
char * filename = NULL;
do
{
if ( (finddata.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE )==
FILE_ATTRIBUTE_ARCHIVE )
{
filename = new char [500];
strcpy ( filename , finddata.cFileName );
filenames.push_back ( filename );

}

} while ( FindNextFileA( filehandle, & finddata ) != 0 );
FindClose ( filehandle );

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:OV*************@TK2MSFTNGP03.phx.gbl...
Carl Daniel [VC++ MVP] wrote:
Abubakar wrote:
Sorry for the late reply.

How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.

I use the "remove" method.

I have the following declaration:

list<char * >::const_iterator name, prev;

at some point I do:

prev = name;
name ++;
filenames.remove ( *prev );

and this is how I remove the items.
If you have duplicates in your list, the call to remove will remove
more than one of them at a time, including possibly the one now
referenced by "name". Use the loop that Bo suggested:

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

that's the canonical way to iterate through a list removing some
elements as you go.

Also, how are you adding the items to the list? Since you're storing raw
char*'s, the list isn't managing the memory occupied by the strings - you
are. You should consider changing to a std::list<std::stringinstead.

-cd


Aug 16 '06 #6
Hi,

Ok I'll try the *remove* method of the list and if I still cant get the
begin() method to work right I'll get back to this post.

Regards,

-ab.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:u3**************@TK2MSFTNGP04.phx.gbl...
Abubakar wrote:
Sorry for the late reply.
How are you deleting the items? Just doing filenames.erase(filename)
invalidates the iterator to the deleted object.
I use the "remove" method.

I have the following declaration:

list<char * >::const_iterator name, prev;

at some point I do:

prev = name;
name ++;
filenames.remove ( *prev );

and this is how I remove the items.

If you have duplicates in your list, the call to remove will remove more
than one of them at a time, including possibly the one now referenced by
"name". Use the loop that Bo suggested:

if (some_condition)
filename = filenames.erase(filename);
else
++filename;

that's the canonical way to iterate through a list removing some elements
as
you go.

-cd


Aug 16 '06 #7
Abubakar wrote:
>char*'s, the list isn't managing the memory occupied by the strings
- you are.
list <char * filenames ;

WIN32_FIND_DATAA finddata;
//emailblockslocation
HANDLE filehandle = FindFirstFileA ( m_folderlocation , & finddata );
char * filename = NULL;
do
{
if ( (finddata.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE )==
FILE_ATTRIBUTE_ARCHIVE )
{
filename = new char [500];
This memory is being leaked when you remove an item from the list - nothing
will call delete[] on it (unless there's yet more code that you haven't
shown that does delete it).
strcpy ( filename , finddata.cFileName );
filenames.push_back ( filename );

}

} while ( FindNextFileA( filehandle, & finddata ) != 0 );
FindClose ( filehandle );
I'd recommend changing your container to std::list<std::string>, then the
memory for the string will be automatically freed when an item is removed
from the list.

-cd
Aug 17 '06 #8
I'd recommend changing your container to std::list<std::string>, then the
memory for the string will be automatically freed when an item is removed
yes I was going to write the code for the deletion of char *, but I got
stuck in that filenames.end() condition which btw is still not working. This
std::string idea is good and will reduce my code.

-Ab.

"Carl Daniel [VC++ MVP]" <cp*****************************@mvps.org.nospam >
wrote in message news:uq**************@TK2MSFTNGP06.phx.gbl...
Abubakar wrote:
char*'s, the list isn't managing the memory occupied by the strings
- you are.
list <char * filenames ;

WIN32_FIND_DATAA finddata;
//emailblockslocation
HANDLE filehandle = FindFirstFileA ( m_folderlocation , & finddata );
char * filename = NULL;
do
{
if ( (finddata.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE )==
FILE_ATTRIBUTE_ARCHIVE )
{
filename = new char [500];

This memory is being leaked when you remove an item from the list -
nothing
will call delete[] on it (unless there's yet more code that you haven't
shown that does delete it).
strcpy ( filename , finddata.cFileName );
filenames.push_back ( filename );

}

} while ( FindNextFileA( filehandle, & finddata ) != 0 );
FindClose ( filehandle );

I'd recommend changing your container to std::list<std::string>, then the
memory for the string will be automatically freed when an item is removed
from the list.

-cd


Aug 17 '06 #9
Abubakar wrote:
yes I was going to write the code for the deletion of char *, but I got
stuck in that filenames.end() condition which btw is still not working.
Have you tried what Bo and Carl recommended? Here's a complete example:

for(std::list<std::string>::iterator filename = filenames.begin();
filename != filenames.end(); )
{
if(FileNeedsToBeDeletedCondition)
filename = filenames.erase(filename);
else
++filename;
}

If have a radically different mechanism, it's probably not going to work.

I strongly recommend the book Effective STL by Scott Meyers, which
discusses this and dozens of other tricks with STL containers.

Tom
Aug 17 '06 #10
Hi,

I have solved the problem by using the list::back() method. As back()
represents the last item in the list, so thats what I was looking for. The
problem with what I was doing was my lack of understanding with the list's
"end" method. I thought that the "end" represents the last item, which is
not correct.

Thanks for the book recommendation!

Regards,

-Ab.

PS: again, sorry for having replied so late,,,, I hate my replying-late
habbit !!!

"Tamas Demjen" <td*****@yahoo.comwrote in message
news:OB**************@TK2MSFTNGP05.phx.gbl...
Abubakar wrote:
yes I was going to write the code for the deletion of char *, but I got
stuck in that filenames.end() condition which btw is still not working.

Have you tried what Bo and Carl recommended? Here's a complete example:

for(std::list<std::string>::iterator filename = filenames.begin();
filename != filenames.end(); )
{
if(FileNeedsToBeDeletedCondition)
filename = filenames.erase(filename);
else
++filename;
}

If have a radically different mechanism, it's probably not going to work.

I strongly recommend the book Effective STL by Scott Meyers, which
discusses this and dozens of other tricks with STL containers.

Tom

Aug 19 '06 #11

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

Similar topics

11
by: William Payne | last post by:
Ok, in my program I have a std::list<Document*>, where Document is one of my own classes. I need to go through this list and check each item if it's ready for deletion. If it's not, skip to...
5
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h if there is anybody who could help me with this hw i really...
1
by: jhon02148 | last post by:
hi this hw have four files: 1. for the main program 2. listp.cpp (the source file) 3. listp.h (the header file) 4. exception.h hi iam done with my hw i still have to do one function which is...
13
by: XXXXXX.working.in.my.blood | last post by:
hi all, i need help with linked lists... the problem is this, "reverse the contents of a singly linked list without using a temporary node"... solution with code will be appreciated...
11
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to...
7
by: satan | last post by:
The method remove of the class ArrayListClass removes only the first occurence of an element. Add the method removeAll as an abstract method to the class ArrayListClass that would remove all...
1
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
7
by: satan | last post by:
I need a help with the program to test the insertion, deletion, search, copyList, and the copy constructor operations for an object in the class UnorderedLinkedList. These are my classes: ...
9
by: pic078 via AccessMonster.com | last post by:
I need serious help - I have a frontend/backend Access database (2 MDE Files) that remains stuck in task manager after exiting the application - you can't reopen database after exiting as a result...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.