C++ String termination

D

Dmitry Denisenkov

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
 
D

David Harmon

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.
 
T

Thomas Matthews

Dmitry said:
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

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
 
M

Mike Wahler

Dmitry Denisenkov said:
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
 
D

Dmitry Denisenkov

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
 
J

Jonathan Turkanis

Dmitry Denisenkov said:
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
 
A

Avinash

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;
 
O

Old Wolf

Hi,
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).
 
C

Clark Cox

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.[/QUOTE]

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

Kelsey Bjarnason

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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top