~Dmitry
std::string is not terminated with '\0' or anything else in particular.
It may contain zero characters without restriction. The .c_str() member
function returns a zero- terminated version of the string contents, with
potential for confusion if the string contains any zeros characters.
I do believe that length() and size() are synonyms for the
string class. Many people talk about the _length_ of a string
of text, while others talk about the _size_ of a sequence
or container. Thus both were provided and do the same thing.
--
Thomas Matthews
C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
That's by design.
----------------------------------------------------------------------------
-----
ISO/IEC 14882:1998(E)
21.3.3 basic_string capacity
size_type size() const;
1 Returns: a count of the number of char-like objects currently in the
string.
size_type length() const;
2 Returns: size().
----------------------------------------------------------------------------
-----
> Does that mean that c++ strings are, unlike c strings,
> NOT terminated by char(0)?
It has nothing to do with a 'terminator'. It just means that both
functions return the same value for the same string.
> For some reason I used to think that they are.
No, std::string type does not use a terminator for length determination
as does a 'C style string'. The length (aka size) of a std::string
is reported by the above member functions.
A std::string can contain any character values at all, including
ones with a value of zero.
std::string s(10, 0); /* construct a string containing ten characters, */
/* all of whose value is zero */
std:: cout << s.size() << '\n'; /* prints 10 */
-Mike
Thanks.
~Dmitry
"Mike Wahler" <mkwa...@mkwahler.net> wrote in message
news:%cfWb.23067$uM2....@newsread1.news.pas.earthlink.net...
While your studying these things, you might want to check out c_str()
and data()
Jonathan
"Dmitry Denisenkov" <canwo...@SPAMyahoo.ca> wrote in message news:<p9eWb.29694$964.5065@edtnps84>...
Every implementation I've seen does store a '\0' terminator, so that
c_str() can just return data() . Otherwise, the c_str() function involves
allocating memory, and then there is the thorny issue of when to free
that memory. Is there a portable way of determining whether your
implementation does this? It's useful for making efficiency decisions
(ie. whether it's ok to call c_str() willy-nilly or not).
> > >Hi,
> > >I recently found out that in std::string a("some text"),
> > >a.length()==a.size(). Does that mean that c++ strings are, unlike c
> > >strings,
> > >NOT terminated by char(0)? For some reason I used to think that they are.
> >
> > std::string is not terminated with '\0' or anything else in particular.
> > It may contain zero characters without restriction. The .c_str() member
> > function returns a zero- terminated version of the string contents, with
> > potential for confusion if the string contains any zeros characters.
>
> Every implementation I've seen does store a '\0' terminator, so that
> c_str() can just return data() . Otherwise, the c_str() function involves
> allocating memory, and then there is the thorny issue of when to free
> that memory.
Every implementation that I've seen doesn't store the terminating
'\0' until c_str() is actually called. There is no 'thorny' issue as to
when the memory gets deallocated, it's simply tacked onto the end of the
buffer that string already manages.
> Is there a portable way of determining whether your
> implementation does this?
No, not that I can see, as any test for the terminating '\0' would
invoke undefined behavior if it didn't actually exist (i.e. you'd run
off the end of the buffer).
> It's useful for making efficiency decisions
> (ie. whether it's ok to call c_str() willy-nilly or not).
Just assume that it isn't :)
Offhand, I can't think of any reason a C++ string would _need_ the
terminating 0. Consider, the whole point to the 0 was to indicate the end
of the string - but the C++ string type already knows that, thanks to the
length() member function (or, more likely, a _length member variable).
About the only reason I can see, off the top of my head, for keeping the
terminating 0 is to reduce the overhead in the .c_str() member function;
if the 0 isn't kept, then the function is going to have to add one,
possibly realloc-ing a buffer to do so, which could be rather inefficient.