From 8749539163588283849
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,b83a4267f4ac775
X-Google-Attributes: gidf78e5,public
From: AllanW@my-dejanews.com
Subject: Re: string/cow/algorithm
Date: 1998/08/13
Message-ID: <6qvgqs$otc$1@nnrp1.dejanews.com>#1/1
X-Deja-AN: 380862522
X-NNTP-Posting-Host: 199.125.171.8
Approved: stephen.clamage@sun.com (comp.std.c++)
References: <6qfr41$oue$1@pigpen.csrlink.net> <6qgs0r$92e$1@pigpen.csrlink.net> <35ccc2b4.832003@news3.ibm.net> <6qn19u$sb0$1@pigpen.csrlink.net> <6qoumk$402$1@shell7.ba.best.com> <35D1DA1C.A2B@bean.jpl.nasa.gov>
X-Snookums: I love you
X-UID: 0000000001
X-Status: $$$T
X-Http-User-Agent: Mozilla/4.04 [en] (Win95; I)
Organization: Deja News - The Leader in Internet Discussion
X-Article-Creation-Date: Thu Aug 13 20:03:40 1998 GMT
Newsgroups: comp.std.c++
Originator: clamage@taumet


In article <35D1DA1C.A2B@bean.jpl.nasa.gov>,
  mckelvey@bean.jpl.nasa.gov wrote:
>
> Nathan Myers wrote:
> >
> > John Potter<jpotter@falcon.lhup.edu> wrote:
> > >I received some email responses to the original.  Not seeing any
> > >posts, I will pass on the information to you and others.  Here is
> > >another nasty problem:
> > >
> > > string s("abc");
> > > string::iterator i(s.begin());
> > > string c(s);
> > > *i = 'z';
> > > // What is the value of c[0]?
> > >
> > >A solution is for strings to have one of three states (shared, unique,
> > >unsharable).  You have the first two now.  Any function which returns
> > >a non-const reference to internals must take care of making the copy
> > >and marking it unsharable.  Any copy function which gets an unsharable
> > >to copy must do a deep copy.  In the above, s.begin() would do that
> > >for s and the copy constructor would be forced to do a deep copy in
> > >creating c.
> >
>
> Maybe I'm missing something, but an object should never return a pointer
> or reference to its internals.
>
> If s is a string, an iterator on s would bump the reference counter on
> the value of s. This means that if s changes, the iterator still refers
> to the previous value of s.
>
> The iterator should not be able to change s. It just represents a
> position within s.
>
> So the code becomes something like:
>
> string s("abc");
> string::iterator i(s);
> string c(s);
> s(i) = 'z';
> // What is the value of c[0]?
>
> c[0] is 'a'.
> The only thing changed is s, which is "zbc".

This is true, although the syntax above doesn't show it. If
the implementation of string::iterator contains the address
of the object and an offset then this particular problem is
solved instantly.

But this still doesn't apply to the general case.
    string a("abcdefghijklmnopqrstuvwxyz");
    string b("zyxwvutsrqponmlkjihgfedcba");
    printf("%s", &a[0]);
    char *x = &b[0];
    string c(a);
    string d(b);
    *x = '-';
If taking the address of a character in the string marks the
string "unsharable", then we have four separate strings here.
In this case, ref-counting has been mostly disabled, except
for the extra run-time overhead which is still present.

If we don't mark the string that way just because the user
peeked inside the string, then a/c will (correctly) share
a buffer, and we will get the memory (and possibly performance)
gains that ref-counting promises. Unfortunately, b/d will also
(incorrectly) share a buffer, and changing string b will also
affect string d.

> The call to s(i) can verify that i is indeed an iterator on s. If
> desired, it can then update the iterator to refer to the new value of s.
> In general though, an iterator should be considered a snapshot.

A reasonable idea, but it doesn't use the correct syntax.

> It seems to me that the problem is really that the string class is
> poorly designed.

That does seem to be one of the hot issues of the moment.

--
AllanW@my-dejanews.com is a "Spam Magnet" -- never read.
Please reply in USENET only, sorry.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum


[ 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              ]




