Newsgroups: comp.lang.dylan,comp.lang.misc,comp.lang.lisp,comp.object,comp.arch,comp.lang.c++
Path: cantaloupe.srv.cs.cmu.edu!rochester!udel!news.mathworks.com!newsfeed.internetmci.com!news.sprintlink.net!howland.reston.ans.net!xlink.net!slsv6bt!news
From: kanze@lts.sel.alcatel.de (James Kanze US/ESC 60/3/141 #40763)
Subject: Re: allocator and GC locality (was Re: cost of malloc)
In-Reply-To: "Stefan Monnier"'s message of 18 Aug 1995 12:57:46 GMT
Message-ID: <KANZE.95Aug21155322@slsvhdt.lts.sel.alcatel.de>
Lines: 54
Sender: news@lts.sel.alcatel.de
Organization: SEL
References: <9507261647.AA14556@aruba.apple.com>
	<KANZE.95Aug17141818@slsvhdt.lts.sel.alcatel.de>
	<40vgu4$73r@info.epfl.ch>
	<KANZE.95Aug18133802@slsvhdt.lts.sel.alcatel.de>
	<4122oa$91e@info.epfl.ch>
Date: 21 Aug 1995 13:53:22 GMT
Xref: glinda.oz.cs.cmu.edu comp.lang.dylan:5103 comp.lang.misc:22782 comp.lang.lisp:18882 comp.object:37124 comp.arch:60555 comp.lang.c++:145019

In article <4122oa$91e@info.epfl.ch> "Stefan Monnier"
<stefan.monnier@epfl.ch> writes:

|> In article <KANZE.95Aug18133802@slsvhdt.lts.sel.alcatel.de>,
|> James Kanze US/ESC 60/3/141 #40763 <kanze@lts.sel.alcatel.de> wrote:
|> ] 	extern "C" void     f( char const* s ) ;
|> ] 	string              s ;
|> ] 
|> ] 	f( s.c_str() ) ;
|> ] 
|> ] It's hardly likely that `f' will free it (since in other contexts, `f'
|> ] will be called with e.g.: a string literal), and you don't want to
|> ] hassle having to maintain a pointer yourself.

|> Tell me: how do you do it currently in C ?
|> Don't you have to maintain the pointer yourself ?

There are several solutions in C, all of them problematic.  (The most
frequent seems to be to return a pointer to static memory.)

|> Tell me exactly how "f(s.c_str())" can be correct !
|> c_str has to return a char*, which can either be dynamically allocated or
|> stack-allocated. Stack allocation is incorrect in this case and if you prefer
|> dynamic allocation, then c_str cannot free the char*, it must be f(). So if f()
|> doesn't free the char*, the expression is necessarily wrong !

The pointer returned by c_str belongs to s (the string), and will be
freed when s goes out of scope.

|> The only alternative I can see to what I suggested is for c_str to use a
|> pre-allocated char array which is passed to it as a parameter !

|> ] In the case of the standard string class, it is already documented
|> ] when the pointer returned by c_str becomes invalid.  So you know
|> ] exactly when you can free it.  (If memory serves me right, it is until
|> ] the next non-const function is called on the string, or the
|> ] destructor.)

|> How does this work ? Does it redefine operations on "char*" ?

No, c_str returns a pointer which still belongs to the string, as
explained above.

In the implementations I'm familiar with, the returned pointer is to
the actual internal representation of the string; change the string,
and the pointer is liable to change too.  (This is allowed, but not
required, by the standard.)
-- 
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 en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung


