Article View: comp.std.c++
Article #3112Guarantees concerning references/pointers into string
From: J. Kanze
Date: Thu, 25 Jan 1996 17:46
Date: Thu, 25 Jan 1996 17:46
75 lines
4089 bytes
4089 bytes
I've just tried out the following code:
#include <string>
#include <iostream.h>
int
main()
{
string a , b ;
a = "A lot of junk" ;
a.reserve( 100 ) ;
char* p1 = &a[ 1 ] ;
// b = a ;
a.insert( 6 , "---" ) ;
char* p2 = &a[ 1 ] ;
cout << "Reserve test: " << (p1 == p2 ? "Passed" : "Failed") << endl ;
return 0 ;
}
With the one `standard' string class I have access to, the result is
"Failed".
As I interpret the standard, the above is required to work. The
`Notes' after the function `reserve' state that: ``It is guaranteed
that no reallocation takes place during the insertions that happen
after reserve() takes place till the time when the size of the string
reaches the size specified by reserver().'' From further comments
concerning reallocation, I would conclude that the results of
operator[] should be valid until a reallocation takes place. Is this
the intended interpretation?
Just a guess, but I would imagine from the above, even without seeing
the g++ code, that it uses the widespread copy on write technique. My
own implementation of basic_string< T > uses copy on write, and it was
the amount of effort required to avoid the above problem that suggested
trying it out on someone elses implementation. The presense of
`reserve' makes copy on write anything but trivial to implement
correctly. (The exact case above is actually easy to avoid, and I'm
surprised that g++ has the error. But try uncommenting the assignment
in the above code, and it becomes considerably more difficult to avoid
the problem.)
In a similar veine, what functions are implied by the word `insertion'
in the above quote? I chose `insert' for my test, because if it isn't
an insertion, I don't know what is. What about replace? remove?
assign? non-const at?
One further question occurs: when may reallocation (and the resulting
invalidation of the pointers) occur when reserve has not been called?
For example, is the following guaranteed to work as expected:
string s ;
size_t i , j ;
assert( i < s.size() , j < s.size() ) ;
a[ i ] = a[ j ] ;
I would like for the last statement to be well defined, but according to
my reading of the standard, it isn't. Reallocation is allowed in the
non-const version of operator[] (and must be, if copy on write is to be
a legal implementation). This operator is called twice in the
expression, and the compiler could very easily (and legally) generate
both calls before using either of the results. But if the second call
reallocates, the reference returned by the first call is invalidated.
(I cannot actually conceive of an implementation in which the second
call reallocates, but I cannot find anything in the draft to guarantee
that it won't.)
--
James Kanze (+33) 88 14 49 00 email: kanze@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs Bourgeois, 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. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<KANZE.96Jan25184235@gabi.gabi-soft.fr>Path:
arc1-ca.pugleaf.net!fido.asd.sgi.com!austernShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!fido.asd.sgi.com!austern From: kanze@gabi.gabi-soft.fr (J. Kanze) Newsgroups: comp.std.c++ Subject: Guarantees concerning references/pointers into string Date: 25 Jan 1996 09:46:59 PST Organization: GABI Software, Sarl. Approved: austern@isolde.mti.sgi.com Message-ID: <KANZE.96Jan25184235@gabi.gabi-soft.fr> NNTP-Posting-Host: isolde.mti.sgi.com X-Original-Date: 25 Jan 1996 17:42:35 GMT X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBVAwUBMQfCP0y4NqrwXLNJAQGzgAIAw5JsKqmY9ANtlWtE9hjaWzvQMN+qo8x4 jL9LDjY6T2vR92X71vWwEdkIzOWy3nvVYAlBNbfIEOZTez56bYWPlg== =5G9Y Originator: austern@isolde.mti.sgi.com