473,569 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

list::splice and iterator invalidation

What should be the output of the following program? The SGI STL docs
say that all iterators remain valid during a splice operation. The
dinkumware library reference says that iterators to spliced elts. may be
invalidated. I have one system (Linux gcc) which outputs:

0
3

and another (HP aCC) which outputs:

0
2

Any clarification would be most appreciated.

Thanks,
Mark

// pasted code

#include <iostream>

using namespace std;

struct S
{
S(int i) : i(i) {}
int i;
};

typedef list<S> sl;
typedef sl::iterator sli;

void print(sl& l)
{
for (sli it = l.begin(); it != l.end(); ++it)
cout << it->i << endl;
}

int main()
{

sl a;
a.push_back(0);
sl b;
b.push_back(1);

sli bi = b.begin();
*bi = 2;

a.splice(a.end( ),b);
*bi = 3;

print(a);
}
Sep 20 '05 #1
2 2534
* Mark P:
The SGI STL docs
say that all iterators remain valid during a splice operation. The
dinkumware library reference says that iterators to spliced elts. may be
invalidated.


The standard says, for the two-argument version of splice, "Invalidate s all
iterators and references to the list x", where x is the second argument, and
where presumably "to the list" means "to elements of the list", not to the
list itself.

This unclarity is to my mind an obvious defect in the standard, as is also the
inclusion of references in the things to be invalidated: constant time means
no copying, just pointer shuffling, and so raw pointers and references to
elements should not, as I see it, be invalidated, and _if_ references are
invalidated, then also raw pointers should be invalidated. But anyway,
iterators are certainly invalidated. Unless the whole paragraph is botched.

Thus you have Undefined Behavior, and both implementations you tried are
Right.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 20 '05 #2
Alf P. Steinbach wrote:
* Mark P:
The SGI STL docs
say that all iterators remain valid during a splice operation. The
dinkumware library reference says that iterators to spliced elts. may be
invalidated .

The standard says, for the two-argument version of splice, "Invalidate s all
iterators and references to the list x", where x is the second argument, and
where presumably "to the list" means "to elements of the list", not to the
list itself.

This unclarity is to my mind an obvious defect in the standard, as is also the
inclusion of references in the things to be invalidated: constant time means
no copying, just pointer shuffling, and so raw pointers and references to
elements should not, as I see it, be invalidated, and _if_ references are
invalidated, then also raw pointers should be invalidated. But anyway,
iterators are certainly invalidated. Unless the whole paragraph is botched.

Thus you have Undefined Behavior, and both implementations you tried are
Right.


This is a known defect

http://www.open-std.org/jtc1/sc22/wg...fects.html#250

and

http://www.open-std.org/jtc1/sc22/wg...fects.html#278

john
Sep 20 '05 #3

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

Similar topics

7
4325
by: sam | last post by:
Hi, This is a "list iterator" problem I expect it will copy the list iterator (l_iter) to the caller: eg. list<HashMap>::iterator AcctConfParser::find_Acct_rule(string i) { list<HashMap>::iterator l_iter; HashMap::iterator m_iter; for (l_iter=macro_list.begin(); l_iter!=macro_list.end(); l_iter++) {
15
19696
by: sandwich_eater | last post by:
I want to know how to set an std::list iterator variable to make it null or nil. If this is not possible what is the value of an uninitialised std::list iterator and is it ok to assign this value to a std::list iterator variable (or is it better to use a seperate bool variable as a "null flag" ?) e.g. struct mystru { std::string nm;
5
22824
by: gerg | last post by:
I'm having to deal with some legacy code which I think may be causeing some problems. Can STL pro's please add comments about the correctness of the following code: class A { public: /// ... assume constructors and such are defined char* sz; // pointer to memory buffer };
8
16939
by: freckred76 | last post by:
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 {
3
23497
by: Amit Bhatia | last post by:
User-Agent: OSXnews 2.081 Xref: number1.nntp.dca.giganews.com comp.lang.c++:818044 Hi, I am wondering if I can assign a list iterator = NULL. Suppose I have a class A: A.h class A{ //ctors etc etc.
6
3705
by: Jason S | last post by:
where is the behavior of list::iterator specified? I need to keep track of ranges of data, all I can figure out so far is that iterators are guaranteed to be valid unless you remove the elements they point to. Example -- if L is a list and Li = L.end(), and you call L.push_back(something), how do you know whether Li will still point to the...
15
2770
by: jayesah | last post by:
Hi All, List and its iterator work as following way : list<intmylist; list<int>::iterator itr; itr = mylist.begin(); cout << (*itr); But I want something like this:
2
3661
by: Rohit.MEHTA | last post by:
Hi All I want to overload the ++ and - operator for stl list iterator so that I could do some thing additional in that As want to set the current node status also Can some one please post sample code to use this, I would NOT like to do it by inheriting the iterator class. Rather I would like to keep a member iterator and wrap the logic...
1
6229
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 library from the higher level classes. This low level class is used to allocate locked pages of memory before doing a DMA. The third party library...
19
5391
by: Juha Nieminen | last post by:
If I'm not completely mistaken, the only reason why std::list::size() may be (and usually is) a linear-time operation is because they want std::list::splice() to be a constant-time operation, and if you execute the latter, the size of the resulting lists cannot be known without explicitly counting the sizes of the new lists. I was thinking:...
0
7926
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7679
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5223
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2117
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1228
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
946
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.