From: James Kanze US/ESC 60/3/141 #40763
Subject: Re: allocator and GC locality (was Re: cost of malloc)
Date: 
Message-ID: <KANZE.95Aug18133802@slsvhdt.lts.sel.alcatel.de>
In article <··········@info.epfl.ch> "Stefan Monnier"
<··············@epfl.ch> writes:

|> In article <···················@slsvhdt.lts.sel.alcatel.de>,
|> James Kanze US/ESC 60/3/141 #40763 <·····@lts.sel.alcatel.de> wrote:
|> ] I sort of agree.  Still, the string class will have to manage the
|> ] memory for the flat representation as well.

|> Big deal: malloc the thing, return the pointer and document the fact that
|> whenever the string is not useful any more, it has to be freed by "delete".

Not acceptable.  Obtain the memory through new, and document under
which cases the string class frees it.

Note that its most frequent use will be:

	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.

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.)
-- 
James Kanze         Tel.: (+33) 88 14 49 00        email: ·····@gabi-soft.fr
GABI Software, Sarl., 8 rue des Francs-Bourgeois, F-67000 Strasbourg, France
Conseils en informatique industrielle --
                              -- Beratung in industrieller Datenverarbeitung
From: Stefan Monnier
Subject: Re: allocator and GC locality (was Re: cost of malloc)
Date: 
Message-ID: <4122oa$91e@info.epfl.ch>
In article <···················@slsvhdt.lts.sel.alcatel.de>,
James Kanze US/ESC 60/3/141 #40763 <·····@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 ?

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 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*" ?


	Stefan