473,657 Members | 2,513 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the difference between string::c_str() and string::data()?

In the <basic_string.h >, I find the implementation of these two
functions. But I can't understand the difference between them.
Please give me some help!
//basic_string::c _str()
const _CharT*
c_str() const
{
// MT: This assumes concurrent writes are OK.
size_type __n = this->size();
traits_type::as sign(_M_data()[__n], _Rep::_S_termin al);
return _M_data();
}

//basic_string::d ata()
const _CharT*
data() const { return _M_data(); }

Thanks!

Aug 23 '05 #1
18 7818

Metro12 skrev:
In the <basic_string.h >, I find the implementation of these two
functions. But I can't understand the difference between them.
Please give me some help!
A std::string is not null-terminated. While both functions return a
pointer to an array of the characters, std::string::c_ str() make sure
that the array ends with \0.

/Peter

[snip] Thanks!


Aug 23 '05 #2
On 23 Aug 2005 00:46:33 -0700, "Metro12" <sa*******@yaho o.com.cn>
wrote:
In the <basic_string.h >, I find the implementation of these two
functions. But I can't understand the difference between them.
Please give me some help!
//basic_string::c _str()
const _CharT*
c_str() const
{
// MT: This assumes concurrent writes are OK.
size_type __n = this->size();
traits_type::as sign(_M_data()[__n], _Rep::_S_termin al);
return _M_data();
}

//basic_string::d ata()
const _CharT*
data() const { return _M_data(); }

Thanks!


One is guaranteed by the C++ standard to be null-terminated, the other
isn't (but might be).

--
Bob Hairgrove
No**********@Ho me.com
Aug 23 '05 #3
On Tue, 23 Aug 2005 09:55:35 +0200, Bob Hairgrove
<in*****@bigfoo t.com> wrote:
In the <basic_string.h >, I find the implementation of these two
functions. But I can't understand the difference between them.

[snip]
One is guaranteed by the C++ standard to be null-terminated, the other
isn't (but might be).


Also, since std::string can contain binary data, including the null
character, the data() function would be more useful in those
situations. The pointer returned by c_str(), OTOH, is guaranteed to be
null-terminated and is typically used in legacy code which deals with
textual strings.

Although it would seem that c_str() could be used in either situation,
data() might NOT be null-terminated depending on the implementation.

--
Bob Hairgrove
No**********@Ho me.com
Aug 23 '05 #4
pe************* **@gmail.com wrote:
Metro12 skrev:
In the <basic_string.h >, I find the implementation of these two
functions. But I can't understand the difference between them.
Please give me some help!


A std::string is not null-terminated. While both functions return a
pointer to an array of the characters, std::string::c_ str() make sure
that the array ends with \0.


Not null terminating std::string is impractical. We have yet to see a
std::string without null terminator.

Aug 23 '05 #5

Maxim Yegorushkin skrev:
pe************* **@gmail.com wrote:
Metro12 skrev:
In the <basic_string.h >, I find the implementation of these two
functions. But I can't understand the difference between them.
Please give me some help!


A std::string is not null-terminated. While both functions return a
pointer to an array of the characters, std::string::c_ str() make sure
that the array ends with \0.


Not null terminating std::string is impractical. We have yet to see a
std::string without null terminator.


This is an implementation-detail. I agree that the implementor of
std::string normally would reserve an extra char (and perhaps put \0 in
it) in order not to "get into troubles" if the user calls c_str.
But that 0 is not officially part of the string. Assume:

std::string s("Hello");
if (s[5] == '\0') ...

This last line is undefined behaviour although i would not be surprised
if the comparison was true.
/Peter

Aug 23 '05 #6
On 23 Aug 2005 03:39:28 -0700, pe************* **@gmail.com wrote:
std::string s("Hello");
if (s[5] == '\0') ...

This last line is undefined behaviour although i would not be surprised
if the comparison was true.


On a conforming implementation, this throws std::out_of_ran ge (see
21.3.4), therefore the behavior is defined.

--
Bob Hairgrove
No**********@Ho me.com
Aug 23 '05 #7

pe************* **@gmail.com wrote:
Maxim Yegorushkin skrev:


<snip>

Not null terminating std::string is impractical. We have yet to see a
std::string without null terminator.


This is an implementation-detail. I agree that the implementor of
std::string normally would reserve an extra char (and perhaps put \0 in
it) in order not to "get into troubles" if the user calls c_str.
But that 0 is not officially part of the string. Assume:

std::string s("Hello");
if (s[5] == '\0') ...

This last line is undefined behaviour although i would not be surprised
if the comparison was true.


Why do you think last line invokes UB? Sec 21.3.4 clearly says that
behavior
is undefined only if index is greater than size().

Krishanu

Aug 23 '05 #8

Bob Hairgrove skrev:
On 23 Aug 2005 03:39:28 -0700, pe************* **@gmail.com wrote:
std::string s("Hello");
if (s[5] == '\0') ...

This last line is undefined behaviour although i would not be surprised
if the comparison was true.
On a conforming implementation, this throws std::out_of_ran ge (see
21.3.4), therefore the behavior is defined.


I do not use the constant of operator[]. I do not have the standard
myself, but according to Josuttis book this implies that my index
should be between 0 and s.size() - 1. So you claim is that Josuttis is
wrong? In that case I (and Josuttis ;-) will stand corrected.

/Peter

--
Bob Hairgrove
No**********@Ho me.com


Aug 23 '05 #9

peter.koch.lar. ..@gmail.com skrev:
I do not use the constant of operator[].

should read:
I do not use the constant version of operator[].
/Peter

Aug 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2568
by: Showjumper | last post by:
Whats the diff between the 2 and when do you use one and not the other?
2
1827
by: Asha | last post by:
greetings, should i do this obj.dispose then obj = nothing both does the same thing right? whats the core difference?
4
4030
by: jakk | last post by:
hello all, whats the Difference between bin and obj directories and difference between project references and dll references? thanks jack
3
19281
by: Amir Shitrit | last post by:
Hi to all. Whats the difference between DynamicInvoke and Invoke methods of the Delegate class? Thanks in advance.
4
1793
by: Arjen | last post by:
Hi, Whats the difference between Request.IsAuthenticated and User.Identity.IsAuthenticated? Thanks! Arjen
2
2350
by: Ulrike Klusik | last post by:
Hello Folks, i've got two structural identical tables (including tablespace and indexes) with identical data, on which the access path of an SQL is differs. But I don't see a reason for the different behaviour. Both tables are allocated in the same database and storage space and use the same buffer pool. The DB2 subsystem is Version7.1 on z/OS. On both tables I've run REORG with standard statistics. I've checked for stale statistics...
2
5283
by: czi02 | last post by:
Hi there;; Im wondering whats the difference between vb to visual basic. net??? Does the program was different to visual basic than vb.net ???
1
1987
by: yogesh | last post by:
i have a code as follows TServerDB() { fDB = Server()->dbStorage->Create(); } or fDB=TServer->TMySQLBD->Create(); can any one tell the relationship between two and whats the <between braces called
2
2509
by: czi02 | last post by:
Whats the difference between vb6.0 and a vb.net? Does the same language has an crystal report ? How can I make an crystal report?
10
3431
by: Ahmad Humayun | last post by:
Whats the difference between: char str1 = "wxyz"; char* str2 = "abcd"; I can do this: str2 = str1 but I can't do this: str1 = str2
0
8842
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8740
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1970
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.