Topic: strings and x[i] = '\0
Author: Eric Gindrup <gindrup@okway.okstate.edu>
Date: 1996/08/06 Raw View
Bill Dimm <billd@gim.net> wrote:
[...]
> string x("one");
> x[1] = '\0';
> cout << x.length() << endl;
To paraphrase what I assume many others will attack:
Stomping on your literals is bad juju.
-- Eric Gindrup ! gindrup@okway.okstate.edu
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Date: 1996/08/06 Raw View
In article <3206A48A.6C18@okway.okstate.edu> Eric Gindrup
<gindrup@okway.okstate.edu> writes:
|> Bill Dimm <billd@gim.net> wrote:
|> [...]
|> > string x("one");
|> > x[1] = '\0';
|> > cout << x.length() << endl;
|> To paraphrase what I assume many others will attack:
|> Stomping on your literals is bad juju.
But what does this have to do with modifying string literals. The above
program is well defined; the programmer has changed the second character
of a three character string from 'n' to '\0'. (He has, of course, not
changed the length of the string.)
--
James Kanze Tel.: (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils, itudes et rialisations en logiciel orienti objet --
-- A la recherche d'une activiti dans une region francophone
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: Bill Dimm <billd@gim.net>
Date: 1996/07/30 Raw View
Looking at the April 95 ANSI draft I don't see any
language that prohibits the user from doing this (and
expecting it to work):
string x("one");
x[1] = '\0';
cout << x.length() << endl;
Is the string object required to get the length right under
such circumstances? This would seem to make it impractical to
store the length of the string in the object. Can anyone
provide clarification?
Thanks,
Bill Dimm billd@gim.net
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: tony@online.tmx.com.au (Tony Cook - Stream America)
Date: 1996/07/30 Raw View
Bill Dimm (billd@gim.net) wrote:
: Looking at the April 95 ANSI draft I don't see any
: language that prohibits the user from doing this (and
: expecting it to work):
: string x("one");
: x[1] = '\0';
: cout << x.length() << endl;
: Is the string object required to get the length right under
: such circumstances? This would seem to make it impractical to
: store the length of the string in the object. Can anyone
: provide clarification?
The length should still be 3, AFAIK. The string should contain 'o',
'\0' and 'e'.
--
Tony Cook - tony@online.tmx.com.au
100237.3425@compuserve.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: Matt Pharr <mmp@aperture.Stanford.EDU>
Date: 1996/08/01 Raw View
kanze@gabi-soft.fr (J. Kanze) writes:
> Bill Dimm <billd@gim.net> writes:
> > Looking at the April 95 ANSI draft I don't see any
> > language that prohibits the user from doing this (and
> > expecting it to work):
> >
> > string x("one");
> > x[1] = '\0';
> > cout << x.length() << endl;
> >
> > Is the string object required to get the length right under
> > such circumstances? This would seem to make it impractical to
> > store the length of the string in the object. Can anyone
> > provide clarification?
>
> What's to clarify? The above bit of code sould output 3, and the
> string object x contains three characters: 'o', '\0', and 'e', in that
> order. My conclusion, however, is just the opposite of yours: this
> functionality requires storing the length of the string in the object.
Sounds reasonable, but what does the standard say about string::c_str()'s
behavior here? It seems to follow from the above that:
strlen(some_string.c_str()) == some_string.size()
is not necessarily true. Is that right?
-matt
--
Matt Pharr | mmp@lux.stanford.edu | <URL:http://www-graphics.stanford.edu/~mmp>
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jbc@ElSegundoCA.NCR.COM (Jim Chapman)
Date: 1996/08/01 Raw View
In article 2941@gim.net, Bill Dimm <billd@gim.net> () writes:
> Looking at the April 95 ANSI draft I don't see any
> language that prohibits the user from doing this (and
> expecting it to work):
>
> string x("one");
> x[1] = '\0';
> cout << x.length() << endl;
>
> Is the string object required to get the length right under
> such circumstances? This would seem to make it impractical to
> store the length of the string in the object. Can anyone
> provide clarification?
>
> Thanks,
> Bill Dimm billd@gim.net
Looking at the same draft, I see nothing about '\0' having
special significance. The string class is not required to
store a null to delimit the end of the string. In fact,
the implementation I use does not.
---
---------------
Jim Chapman NCR Corporation
jbc@ElSegundoCA.NCR.COM Parallel Systems Division
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: fjh@mundook.cs.mu.OZ.AU (Fergus Henderson)
Date: 1996/08/01 Raw View
Matt Pharr <mmp@aperture.Stanford.EDU> writes:
>strlen(some_string.c_str()) == some_string.size()
>
>is not necessarily true. Is that right?
Yes. If the string contains '\0' characters, then
strlen(some_string.c_str()) < some_string.size().
--
Fergus Henderson <fjh@cs.mu.oz.au> | "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh> | of excellence is a lethal habit"
PGP: finger fjh@128.250.37.3 | -- the last words of T. S. Garp.
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]
Author: kanze@gabi-soft.fr (J. Kanze)
Date: 1996/08/05 Raw View
Matt Pharr <mmp@aperture.Stanford.EDU> writes:
> kanze@gabi-soft.fr (J. Kanze) writes:
> > Bill Dimm <billd@gim.net> writes:
> > > Looking at the April 95 ANSI draft I don't see any
> > > language that prohibits the user from doing this (and
> > > expecting it to work):
> > >
> > > string x("one");
> > > x[1] = '\0';
> > > cout << x.length() << endl;
> > >
> > > Is the string object required to get the length right under
> > > such circumstances? This would seem to make it impractical to
> > > store the length of the string in the object. Can anyone
> > > provide clarification?
> >
> > What's to clarify? The above bit of code sould output 3, and the
> > string object x contains three characters: 'o', '\0', and 'e', in that
> > order. My conclusion, however, is just the opposite of yours: this
> > functionality requires storing the length of the string in the object.
>
> Sounds reasonable, but what does the standard say about string::c_str()'s
> behavior here?
string::c_str() will return a pointer to four bytes, containing 'o',
'\0', 'e' and '\0', in that order.
> It seems to follow from the above that:
>
> strlen(some_string.c_str()) == some_string.size()
>
> is not necessarily true. Is that right?
This is correct. Why would you expect it to be systematically true?
AFAICS, c_str is a (necessary) hack, to support interfacing C functions
*WHEN* the string is compatible with those functions. In general, I
would consider using this function in any other case (not interfacing a
C function, or with a string that contains anything but "ordinary"
characters) questionable practice at best.
Obviously, if I have a string, and want to resize it, I will use
string::resize (which has the added advantage of working even when the
new size is longer than the current size), and not try hacking the
internal representation with operator[].
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 67000 Strasbourg, France
Conseils en informatique industrielle --
-- Beratung in industrieller Datenverarbeitung
---
[ 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 ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]