Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ String termination

16 views
Skip to first unread message

Dmitry Denisenkov

unread,
Feb 10, 2004, 6:52:53 PM2/10/04
to
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.

~Dmitry


David Harmon

unread,
Feb 10, 2004, 7:02:50 PM2/10/04
to
On Tue, 10 Feb 2004 23:52:53 GMT in comp.lang.c++, "Dmitry Denisenkov"
<canwo...@SPAMyahoo.ca> was alleged to have written:

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.

Thomas Matthews

unread,
Feb 10, 2004, 7:13:44 PM2/10/04
to

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

Mike Wahler

unread,
Feb 10, 2004, 8:04:59 PM2/10/04
to
"Dmitry Denisenkov" <canwo...@SPAMyahoo.ca> wrote in message
news:p9eWb.29694$964.5065@edtnps84...

> Hi,
> I recently found out that in std::string a("some text"),
> a.length()==a.size().

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


Dmitry Denisenkov

unread,
Feb 10, 2004, 9:05:54 PM2/10/04
to
The reason I mentioned length() and size() here was that I wrongly assumed
that std::string has to be terminated by 0, and I expected length() to
return the size of the string not counting the last terminating character
(that is, size()-1), similarly to strlen(char*). But now I see that I was
wrong. I didn't know that length() is a synonym of size().

Thanks.

~Dmitry

"Mike Wahler" <mkwa...@mkwahler.net> wrote in message
news:%cfWb.23067$uM2....@newsread1.news.pas.earthlink.net...

Jonathan Turkanis

unread,
Feb 10, 2004, 9:09:23 PM2/10/04
to
"Dmitry Denisenkov" <canwo...@SPAMyahoo.ca> wrote in message
news:66gWb.25503$Qa3.8725@edtnps89...

> The reason I mentioned length() and size() here was that I wrongly
assumed
> that std::string has to be terminated by 0, and I expected length()
to
> return the size of the string not counting the last terminating
character
> (that is, size()-1), similarly to strlen(char*). But now I see that
I was
> wrong. I didn't know that length() is a synonym of size().
>

While your studying these things, you might want to check out c_str()
and data()

Jonathan


Avinash

unread,
Feb 11, 2004, 1:13:22 AM2/11/04
to
Exactly there is difference between C++ string and C string, there is
no as such so called String in C, but using char* we can achieve
string simulation at some degree. C++ strings is template class
defined as
typedef basic_string <char> string;


"Dmitry Denisenkov" <canwo...@SPAMyahoo.ca> wrote in message news:<p9eWb.29694$964.5065@edtnps84>...

Old Wolf

unread,
Feb 11, 2004, 9:24:02 PM2/11/04
to
> >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. 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).

Clark Cox

unread,
Feb 11, 2004, 9:44:52 PM2/11/04
to
In article <843a4f78.04021...@posting.google.com>,
old...@inspire.net.nz (Old Wolf) wrote:

> > >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 :)

Kelsey Bjarnason

unread,
Feb 13, 2004, 8:49:12 AM2/13/04
to

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.


0 new messages