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

end of std::list

Without further ado, here's some code:

std::list<int> things;

things.push_back(1);
things.push_back(2);
things.push_back(3);

std::list<int>::iterator it;
int test;

it = things.end();
test = *it; // obviously garbage

things.push_back(4);
test = *it; // garbage - but I was expecting '4'

I assumed that 'list' maintained an extra empty node at the end, which is
what gets returned by list.end(). Then I'd expect the 'push_back(4)' to
actually attach the value 4 to that node, which should then be returned in
the second 'test = *it' statement.

Anyone care to enlighten me?
thanks,
G.A.
Jul 22 '05 #1
5 1881

"Glen Able" <sm*************@hotmTHISail.com> wrote in message
news:cj*******************@news.demon.co.uk...
Without further ado, here's some code:

std::list<int> things;

things.push_back(1);
things.push_back(2);
things.push_back(3);

std::list<int>::iterator it;
int test;

it = things.end();
test = *it; // obviously garbage
Actually undefined behaviour.

things.push_back(4);
test = *it; // garbage - but I was expecting '4'
I assumed that 'list' maintained an extra empty node at the end, which is
what gets returned by list.end(). Then I'd expect the 'push_back(4)' to
actually attach the value 4 to that node, which should then be returned in the second 'test = *it' statement.

Anyone care to enlighten me?
thanks,
G.A.


This is covered by the 'invalidation of iterators' requirements that the STL
has. The requirements are different for different containers but for
std::list then only way to invalidate an iterator is to erase the element
that the iterator is pointing to. In all other cases iterators remain valid,
so for a std::list the return from end() will be the end of the list no
matter how many items are subsequently added or removed from the list.

john
Jul 22 '05 #2
"Glen Able" <sm*************@hotmTHISail.com> writes:
Without further ado, here's some code:

std::list<int> things;

things.push_back(1);
things.push_back(2);
things.push_back(3);

std::list<int>::iterator it;
int test;

it = things.end();
test = *it; // obviously garbage

things.push_back(4);
test = *it; // garbage - but I was expecting '4'


AFAIK end() returns the data of an fixed node in the list(a member), and
so end() is always the same node.

Kind regards,
Nicolas

--
| Nicolas Pavlidis | Elvis Presly: |\ |__ |
| Student of SE & KM | "Into the goto" | \|__| |
| pa****@sbox.tugraz.at | ICQ #320057056 | |
|-------------------University of Technology, Graz----------------|
Jul 22 '05 #3

"Glen Able" <sm*************@hotmTHISail.com> schrieb im Newsbeitrag
news:cj*******************@news.demon.co.uk...
Without further ado, here's some code:

std::list<int> things;

things.push_back(1);
things.push_back(2);
things.push_back(3);

std::list<int>::iterator it;
int test;

it = things.end();
test = *it; // obviously garbage

things.push_back(4);
test = *it; // garbage - but I was
expecting '4'


then use:
test = things.back(); and front(); they both give a reference to the
last/first element.
Jul 22 '05 #4
Glen Able wrote:

Without further ado, here's some code:

std::list<int> things;

things.push_back(1);
things.push_back(2);
things.push_back(3);

std::list<int>::iterator it;
int test;

it = things.end();
test = *it; // obviously garbage

things.push_back(4);
test = *it; // garbage - but I was expecting '4'

I assumed that 'list' maintained an extra empty node at the end, which is
what gets returned by list.end(). Then I'd expect the 'push_back(4)' to
actually attach the value 4 to that node, which should then be returned in
the second 'test = *it' statement.


Where is it stated, that it has to be that way.

A list can also always use the very same node for marking the end of list
(if there is such a node at all, I don't think that dereferencing the end
iterator is an allowed operation. But I'm not sure about that)

If you push_back, a new node gets allocated and inserted just before that
end_of_list node.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5
"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:41***************@gascad.at...
Where is it stated, that it has to be that way.

A list can also always use the very same node for marking the end of list
(if there is such a node at all, I don't think that dereferencing the end
iterator is an allowed operation. But I'm not sure about that)

If you push_back, a new node gets allocated and inserted just before that
end_of_list node.


Bah. My actual application is trying to store references to positions in a
list (previously I was using a std::vector and indices). Problem is where
sometimes I need to add a reference to the place in the list where the next
element is going to be added, whenever that is. My naive interpretation of
'list iterators are never invalidated' lead me astray!

I guess I'll have to wrap the list in a class which has a collection of
references which need to be set to the next list element, whenever it gets
appended.

Thanks everyone.
G.A.
Jul 22 '05 #6

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

Similar topics

3
by: Mike Pemberton | last post by:
I'm sure there's a good explanation for this effect, but I get rather a strange output from this little test: #include <iostream> #include <list> int main() { std::list<int> int_list;
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.