I'm curious about the performance of string::c_str,
so I'm wondering how it's commonly implemented. Do
most std::string implementations just keep an extra
char allocated for the NULL termination so they can
return a pointer to their internal buffer, or are
they equally likely to create a new buffer on demand?
I know the standard doesn't require any particular
implementation, which is why I'm curious if there is
a consensus among implementations .
Jul 22 '05
15 11612
On Tue, 25 May 2004 17:13:53 +0000 (UTC), Christopher Benson-Manica
<at***@nospam.c yberspace.org> wrote: Leor Zolman <le**@bdsoft.co m> spoke thus:
Ah yes, of course it /could/ be set up that way, and it does help to understand that the Standard allows for this. But it also does seem that the practical concern most folks run up against when first exposed to c_str() runs along the lines of wanting to know its potential cost under the real platform in use; empirically, the call is an effective freebie.
Does "empiricall y" include implementations as old as 1999? And don't think I'm talking about my implementation again, I never talk about my implementati on ;)
VC6 was one of the ones I checked, and pjp's copyright notice says 1994.
Back before Dinkumware adopted the small-string optimization, c_str() was
even simpler than it is now (but not by much; they're both "simple").
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
On Tue, 25 May 2004 10:17:18 -0700, Andrey Tarasevich
<an************ **@hotmail.com> wrote: Leor Zolman wrote: ... The basic_string object can delete the buffer at those points if it allocated one, or it can wait until the next call to c_str or until destructio n. Not that anybody does it that way, of course. But the idea was that basic_string could be implemented to hold its text in non-contiguous chunks, and only be required to gather their contents together on a call to c_str.
Ah yes, of course it /could/ be set up that way, and it does help to understand that the Standard allows for this. But it also does seem that the practical concern most folks run up against when first exposed to c_str() runs along the lines of wanting to know its potential cost under the real platform in use; empirically, the call is an effective freebie.
Unfortunatel y, in many cases (and I've seen it more than once) once some folks find out that in practice the pointer returned by 'c_str()' really points to the beginning of the actual controlled sequence, they proceed right on to casting away the constness and using the resultant pointer to modify the stored string. In my opinion, in order to keep beginner C++ programmers from getting this nasty habit it might be quite useful to continue to perpetuate the idea that 'c_str()' might return a pointer to a independently allocated buffer (which is, BTW, at least partially true in implementations that return a pointer to a static empty-string literal "" for 'std::string's of zero length).
How does perpetuating the idea that the buffer is separate serve to
discourage anyone from casting away the constness of the pointer? If
anything, it would /encourage/ the practice, since it implies that there's
now a copy that can be "freely" modified without affecting the original
string. If they're going to go circumvent the safeguards that are in place,
let's at least make sure they're cognizant of their actual
transgressions. ..
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
Leor Zolman wrote: ... Unfortunately , in many cases (and I've seen it more than once) once some folks find out that in practice the pointer returned by 'c_str()' really points to the beginning of the actual controlled sequence, they proceed right on to casting away the constness and using the resultant pointer to modify the stored string. In my opinion, in order to keep beginner C++ programmers from getting this nasty habit it might be quite useful to continue to perpetuate the idea that 'c_str()' might return a pointer to a independently allocated buffer (which is, BTW, at least partially true in implementations that return a pointer to a static empty-string literal "" for 'std::string's of zero length).
How does perpetuating the idea that the buffer is separate serve to discourage anyone from casting away the constness of the pointer? If anything, it would /encourage/ the practice, since it implies that there's now a copy that can be "freely" modified without affecting the original string. If they're going to go circumvent the safeguards that are in place, let's at least make sure they're cognizant of their actual transgressions. ..
Well, that only convinces me that both ways to perceive 'c_str()'s
functionality allow for certain misuses. Which one is more "encouragin g"
is a subjective issue.
--
Best regards,
Andrey Tarasevich
On Tue, 25 May 2004 11:08:51 -0700, Andrey Tarasevich
<an************ **@hotmail.com> wrote: Leor Zolman wrote: ... Unfortunatel y, in many cases (and I've seen it more than once) once some folks find out that in practice the pointer returned by 'c_str()' really points to the beginning of the actual controlled sequence, they proceed right on to casting away the constness and using the resultant pointer to modify the stored string. In my opinion, in order to keep beginner C++ programmers from getting this nasty habit it might be quite useful to continue to perpetuate the idea that 'c_str()' might return a pointer to a independently allocated buffer (which is, BTW, at least partially true in implementations that return a pointer to a static empty-string literal "" for 'std::string's of zero length).
How does perpetuating the idea that the buffer is separate serve to discourage anyone from casting away the constness of the pointer? If anything, it would /encourage/ the practice, since it implies that there's now a copy that can be "freely" modified without affecting the original string. If they're going to go circumvent the safeguards that are in place, let's at least make sure they're cognizant of their actual transgressions. ..
Well, that only convinces me that both ways to perceive 'c_str()'s functionalit y allow for certain misuses. Which one is more "encouragin g" is a subjective issue.
I guess. My reasoning went like this: if you think you're getting a
separate buffer, and you cast away the constness of a pointer to it, you'll
have a false sense of security coming from a position of, "Well, I'm just
using a resource that was unreasonably withheld from me for some unknown
archaic Standardese rationale, and I can't really get into any trouble for
doing it". On the other hand, if you realize you're writing into the
internal state of your std::string object, and you do it anyway, at least
you'd know you're doing something seriously unkosher, equivalent to editing
a header file by changing "private:" to "public:".
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html
Pete Becker <pe********@acm .org> wrote in message news:<40******* ********@acm.or g>... Leor Zolman wrote: I looked at about 5, and they all either return the pointer directly, or first do a conditional test and return one of several pointers (I suspect this has to do with the small string optimization). But none of the ones I've looked at go creating a new buffer. I don't see how they /could/, because it would change the dynamics of the pointer returned. The caller would become responsible for deleting the memory, rendering code using that library implementation incompatible with code from "normal" implementations .
Looking up the Standard's spec for c_str (21.3.6), I didn't see anything that explicitly disallows allocating a copy (I perhaps just don't know where to look), but the way it says the pointer will become invalidated after any call to a non-const string member function (on the associated string) pretty much tells the tale. The basic_string object can delete the buffer at those points if it allocated one, or it can wait until the next call to c_str or until destruction. Not that anybody does it that way, of course.
Not with basic_string.
But just for the records: The rope implementation that SGI bundled with
their STL - and I believe it is also included as an extension in GNU
libstdc++, but no longer maintained - uses such a technique.
Rope was intended to be a heavy-duty string, working mostly just
ike a std::string, but optimized for repeated manipulation - copying,
pasting, replacing of substrings - of very long strings. A rope would
start off as a normal string with a contiguous buffer, but after such
manipulations it would consist of a tree - nitpickers would call it a
DAG - of text buffer nodes. The c_str function would have to copy - in
fact, (re)create - the string buffer, with associated performance cost.
Of course, rope is neither standard, nor general-purpose, nor, for
that matter, does it appear to be used anywhere, so discussing its
performance characteristic is not that important - but fun anyway.
But the idea was that basic_string could be implemented to hold its text in non-contiguous chunks, and only be required to gather their contents together on a call to c_str.
Uwe
On Sat, 29 May 2004 22:18:58 GMT, "Daniel T." <po********@eat hlink.net>
wrote: In article <8q************ *************** *****@4ax.com>, Leor Zolman <le**@bdsoft.co m> wrote:
On Tue, 25 May 2004 10:17:18 -0700, Andrey Tarasevich <an********** ****@hotmail.co m> wrote:
Leor Zolman wrote: > ... >The basic_string object can delete the buffer at those points if it >allocate d one, or it can wait until the next call to c_str or until >destructio n. Not that anybody does it that way, of course. But the idea >was that basic_string could be implemented to hold its text in >non-contiguous chunks, and only be required to gather their contents >together on a call to c_str.
Ah yes, of course it /could/ be set up that way, and it does help to understand that the Standard allows for this. But it also does seem that the practical concern most folks run up against when first exposed to c_str() runs along the lines of wanting to know its potential cost under the real platform in use; empirically, the call is an effective freebie.
Unfortunatel y, in many cases (and I've seen it more than once) once some folks find out that in practice the pointer returned by 'c_str()' really points to the beginning of the actual controlled sequence, they proceed right on to casting away the constness and using the resultant pointer to modify the stored string. In my opinion, in order to keep beginner C++ programmers from getting this nasty habit it might be quite useful to continue to perpetuate the idea that 'c_str()' might return a pointer to a independently allocated buffer (which is, BTW, at least partially true in implementations that return a pointer to a static empty-string literal "" for 'std::string's of zero length).
How does perpetuating the idea that the buffer is separate serve to discourage anyone from casting away the constness of the pointer?
Because the user of string doesn't know what the particular implemenatio n he is using will do. If he wants to keep his code portable, he must assume that some strings do, in fact, keep a seperate buffer, while others don't, and act accordingly.
Okay, I'll buy that. In my initial reading of the first of Andrey's posts I
replied to (quoted above), I probably misread "c_str() might return a
pointer..." as "c_str() does return a pointer...". Indeed, as long as you
stress that /you just don't know/, which is essentially true (are there any
reference-counted std::string implementations in use, where assigning
through the return value of c_str() could conceivably modify /several/
string objects in one fell poke?) , it follows that there's just no valid
reason to cast away the constness of that pointer.
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Choubey |
last post by:
hi everybody,
Iam a java programmer actually i want to know how the Connection
object is created through DriverManager.getConnection when
DriverManager class is not implements Connection interface and no class
in java implementas interface connection so how can we get a object
reference for connection object and call a getconnection method through
the connection object. i seen that there are lots of interface in java
which are not...
|
by: Leif K-Brooks |
last post by:
Has anyone ever tried implementing a simple unstructured BASIC dialect
in Python? I'm getting interested in language implementation, and
looking at a reasonably simple example like that could be pretty
interesting.
|
by: Quest Master |
last post by:
I am interested in developing an application where the user has an
ample amount of power to customize the application to their needs, and
I feel this would best be accomplished if a scripting language was
available. However, I want to code this application in Python, and I
have not yet heard of an implementation of another scripting language
into Python.
An example of what I mean is this (an implementation of Lua into Ruby
-- which I'd...
|
by: roy |
last post by:
Hi,
I was wondering how strlen is implemented.
What if the input string doesn't have a null terminator, namely the
'\0'?
Thanks a lot
Roy
|
by: maria.s |
last post by:
Hi,
I try to create a calender-item in the personal calendar folder from an
ASP.NET application using XML-HTTP Request (WebDAV).
System: Windows 2003 SP1, Exchange 2003 SP1
Configuration IIS: Default Web Site stopped, OWA running on a second virtual
site, my application is running on a third virtual site.
| |
by: sujilc |
last post by:
This question seems to be silly. Can ony one figure out the default
functions implemented by compiler when we decalre a class like
class A
{
}
According to me this declaration will define default functions like
|
by: forgroupsonly |
last post by:
Hello All.
I wonder if browsers developers scoff at CSS developers... I do simple
tests while reading CSS2.1 specification, just few boxes. And from time
to time I see that recent browsers render these *simple* things
*incorrectly* (each one in it's own way).
If I had a big page with dozen of blocks and they rendered incorrectly
- I'll call this a *bug* (and I can understand this). But when simple
things from specification not...
|
by: Satish Itty |
last post by:
What is the advantage of the new auto implemented properties introduced
in ocras?
For example is there any difference between
public string Message;
and
public string Message {get;set;}
|
by: Author |
last post by:
I know the basic differences between a struct and a class, but
apparently not good enough to know why some types/concepts are
implemented as struct whereas most types are implemented as class.
For example, why is DateTime implemented as a struct? Why is BitArray
implemented as a class, but BitVector32 a struct?
In general, in OO design, how do we determine if we should use struct
or class?
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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...
|
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,...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |