Topic: iterator problem


Author: "Bill Wade" <bill.wade@stoner.com>
Date: 2000/07/28
Raw View
"John D. Hickin" <hickin@cam.org> wrote

> A quick look at the <string> header shows that begin() and end() invoke a
> copy-on-write _avoidance play_ which is ill taken: there may only be 1
> reference, in which no copy is made. A better approach might have been to
> add 1 to the reference count in the iterator's constructor and to remove 1
> reference in its destructor. This would be nicer than just marking the
> string as non-shareable.

I'm not sure what you get from incrementing the reference count in non-const
begin.  Can you give an example of what you mean (and how it interacts with
writes through the iterator)?




---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/07/21
Raw View
pratik khasnabis wrote:
>
> Following a suggestion from comp.lang.c++ group I'm reposting my problem
> to this group.
>
> The following code is taken from Bruce Eckle's  ' Thinking In C++ '.
> When I compile  this program on a HP-UX 11 machine (% aCC
> StringStorage.cpp)
> and run (% a.out)  I get the following output
>
> %
> s1 = 02345
> s2 = 02345
>
> But according to the book (if I have understood properly) the output
> should be
> %
> s1 = 02345
> s2 = 12345
>
> Which one is the correct behaviour and why ?
>
> Pratik
>
> --------------------------------------------------------------------------------------
>
> //: C01:StringStorage.cpp
> #include <iostream.h>
> #include <string>
>
> //using namespace std;
>
> int main()
> {
>   string s1("12345");
>   // Set the iterator indicate the first element
>   string::iterator it = s1.begin();
>   // This may copy the first to the second or
>   // use reference counting to simulate a copy
>   string s2 = s1;
>   // Either way, this statement may ONLY modify first
>   *it = '0';
>   cout << "s1 = " << s1 << endl;
>   cout << "s2 = " << s2 << endl;
> } ///:~

The relevant parts of that code are almost identical to an example in
section 21.3 p6 of the standard. That example is provided to illustrate
the point that the second string must NOT be modified by the change to
the first. Therefore,
the compiler got it wrong.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: smeyers@aristeia.com (Scott Meyers)
Date: 2000/07/22
Raw View
On Fri, 21 Jul 2000 23:23:29 CST, James Kuyper wrote:
> the first. Therefore,
> the compiler got it wrong.

More precisely, the library got it wrong.  This kind of thing is unlikely to be
a compiler problem.

I think it is important to distinguish between the compiler and the library for
problems like these.

Scott


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "John D. Hickin" <hickin@cam.org>
Date: 2000/07/25
Raw View
From: "Scott Meyers" <smeyers@aristeia.com>
> More precisely, the library got it wrong.  This kind of thing is unlikely
to be
> a compiler problem.
>
> I think it is important to distinguish between the compiler and the
library for
> problems like these.
>

A quick look at the <string> header shows that begin() and end() invoke a
copy-on-write _avoidance play_ which is ill taken: there may only be 1
reference, in which no copy is made. A better approach might have been to
add 1 to the reference count in the iterator's constructor and to remove 1
reference in its destructor. This would be nicer than just marking the
string as non-shareable.


Regards, John.

P.S.: Anybody out there know what has happened to the
comp.lang.c++.moderated newsgroup. I've seen no traffic there for more than
2 days now.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: pratik khasnabis <pratik.khasnabis@tre.ntc.nokia.com>
Date: 2000/07/21
Raw View
Following a suggestion from comp.lang.c++ group I'm reposting my problem
to this group.


The following code is taken from Bruce Eckle's  ' Thinking In C++ '.
When I compile  this program on a HP-UX 11 machine (% aCC
StringStorage.cpp)
and run (% a.out)  I get the following output

%
s1 = 02345
s2 = 02345

But according to the book (if I have understood properly) the output
should be
%
s1 = 02345
s2 = 12345

Which one is the correct behaviour and why ?

Pratik

--------------------------------------------------------------------------------------

//: C01:StringStorage.cpp
#include <iostream.h>
#include <string>

//using namespace std;

int main()
{
  string s1("12345");
  // Set the iterator indicate the first element
  string::iterator it = s1.begin();
  // This may copy the first to the second or
  // use reference counting to simulate a copy
  string s2 = s1;
  // Either way, this statement may ONLY modify first
  *it = '0';
  cout << "s1 = " << s1 << endl;
  cout << "s2 = " << s2 << endl;
} ///:~

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]