On Tuesday 01 March 2005 02:10, Jeff Hill wrote:
> > Jeff, could you please explain why the interface of
> > std::string is not
> > compatible with such implementations?
>
> One of the OO convential wisdoms is to never implement an
> interface that passes off handles to an internal data structure.
> See Item 29 in "Effective C++ 2nd edition", Scott Meyers. To my
> continued amazement there is in std::basic_string a function
> called c_str() which all implementations I have looked at so far
> implement by providing a pointer to the raw internal storage for
> the string. Here is the latest GNU source code. This
> implementation appears to prevent strings stored in
> non-contiguous, fixed sized blocks.
>
> const value_type *c_str( ) const;
>
> // String operations:
> /**
> * @brief Return const pointer to null-terminated
> contents.
> *
> * This is a handle to internal data. Do not modify or
> dire things may
> * happen.
> */
> const _CharT*
> c_str() const { return _M_data(); }
Just because many implementations of std::string implement c_str in this
way doesn't mean it /must/ be implemented this way. An alternative
implementation of std::string, based on fixed-sized non-contiguous
blocks of memory, would need to allocate a new standard c-string
(contiguous), copy the data to it, and pass that as the result of
c_str.
Of course, such an implementation of c_str is a lot less efficient, but
I think this lies in the nature of things. /Any/ implementation of
strings based on non-contiguous blocks will encounter the same problem,
if it wants to provide a routine to interface with routines that expect
a contiguous null-terminated C string.
Ben
- Replies:
- RE: memory management Jeff Hill
- References:
- RE: memory management Jeff Hill
- Navigate by Date:
- Prev:
RE: memory management Jeff Hill
- Next:
RE: memory management Jeff Hill
- Index:
2002
2003
2004
<2005>
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
- Navigate by Thread:
- Prev:
RE: memory management Jeff Hill
- Next:
RE: memory management Jeff Hill
- Index:
2002
2003
2004
<2005>
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
|