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 9 2896
On Tue, 10 Feb 2004 23:52:53 GMT in comp.lang.c++, "Dmitry Denisenkov"
<ca*********@SPAMyahoo.ca> was alleged to have written: 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.
"Dmitry Denisenkov" <ca*********@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
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" <mk******@mkwahler.net> wrote in message
news:%c*******************@newsread1.news.pas.eart hlink.net... "Dmitry Denisenkov" <ca*********@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" <ca*********@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
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" <ca*********@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(). 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
> >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).
In article <84**************************@posting.google.com >, ol*****@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 :)
On Tue, 10 Feb 2004 23:52:53 +0000, Dmitry Denisenkov 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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Espen Ruud Schultz |
last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a
pointer to the std::string's "content" so that the char pointer can point to
the same text? And vice versa; can I give...
|
by: Felix Kater |
last post by:
The C-faq says that "The malloc/free implementation remembers the size
of each block allocated and returned, so it is not necessary to remind
it of the size when freeing."
Could that length...
|
by: junky_fellow |
last post by:
Consider the following piece of code:
char *str = "Hello";
if (str = "Hello")
printf("\nstring matches\n");
str is pointer to char and "Hello" is a string literal whose
type is "array of...
|
by: GB |
last post by:
Hi,
I have a structure which I want to pass as a pointer to a C dll. The
problem is that C# adds '\0' character at the end of each string
(which is contained in the struct).
I understand that...
|
by: copx |
last post by:
Can you / are you supposed to free() string literals which are no longer
needed?
In my case I've menu construction code that looks like this:
menu_items = list_new();
list_add(menu_items,...
|
by: Xah Lee |
last post by:
in March, i posted a essay “What is Expressiveness in a Computer
Language”, archived at:
http://xahlee.org/perl-python/what_is_expresiveness.html
I was informed then that there is a academic...
|
by: robert maas, see http://tinyurl.com/uh3t |
last post by:
I'm working on examples of programming in several languages, all
(except PHP) running under CGI so that I can show both the source
files and the actually running of the examples online. The first...
|
by: indy1000 |
last post by:
Hi,
I'm trying to create a tcp/ip port listening socket server for various
cellular devices that don't use the standard "\r\n" termination string
at the end of their message. Instead, they use...
|
by: Rainman |
last post by:
Hi! I'm new here, and I had my first encounter with this site yesterday, when I shamelessly copied Killer42's excellent code for reading a file using the Get method. Thanks! :-) It worked like a...
|
by: Logan Lee |
last post by:
Hi. I've written a small program to learn to write in C. But unfortunately the output is all jumbled up and not nice.
/* read_file.c
The whole point of this code is to read the entire content...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |