473,805 Members | 2,008 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What storage does std::string::c_ str() use?

All,

If I am not mistaken I had some problems with code like this:

std::string foo, bar;
....
somefunc( foo.c_str(), bar.c_str() );

Problem was that c_str() used buffer shared btw instances of
std::string in that implementation. I did not find anything that
standart would explicitly say about this case. So what would you say?

Thanks,
Slava
Jul 22 '05 #1
2 3096
Vyacheslav Kononenko wrote:
If I am not mistaken I had some problems with code like this:

std::string foo, bar;
...
somefunc( foo.c_str(), bar.c_str() );
What kind of problems? The standard says you cannot rely on those
pointers if you call any non-const member functions for the strings
after obtaining the pointers.
Problem was that c_str() used buffer shared btw instances of
std::string in that implementation. I did not find anything that
standart would explicitly say about this case. So what would you say?


I'd say, you had a terribly buggy implementation.

Victor
Jul 22 '05 #2
vy********@kono nenko.net (Vyacheslav Kononenko) wrote:
All,

If I am not mistaken I had some problems with code like this:

std::string foo, bar;
...
somefunc( foo.c_str(), bar.c_str() );

Problem was that c_str() used buffer shared btw instances of
std::string in that implementation. I did not find anything that
standart would explicitly say about this case. So what would you say?


If foo and bar contain the same string, then you could get the
same buffer, eg:
foo = bar = "hello";
then ( foo.c_str() == bar.c_str() ) is possible.

When writing 'somefunc' you should allow for this possibility.

Also if the strings overlap you could (conceivably -- I don't
know of any implementation that actually does this) get the
same buffer:
foo = "rhombus";
bar = "bus";
then ( foo.c_str() + 4 == bar.c_str() ) could possibly be true.

However the standard requires that the result of c_str() is
valid for a certain time, so if the strings are different and
you get the same buffer, your implementation is broken.
Jul 22 '05 #3

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

Similar topics

19
22743
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 the std::string a pointer and a length and then give the std::string control over the pointer and its content? I'm basically trying to avoid copying large text between an std::string and a char pointer, and vice versa. Is there anyhing in the...
1
1880
by: Christopher Benson-Manica | last post by:
I would like to know whether calling c_str() for a std::string repeatedly (with no intervening non-const function on the string) forces the repopulation of the character array or whether the string can cache the array. I'm guessing that this is a QOI issue, but I figured I'd ask anyway. I realize that I can accomplish the same thing with a const char * here, but the code looks better without it :) --
1
1850
by: vch | last post by:
Is it guaranteed that as long as std::string instance doesn't change it will always return the same pointer with c_str(), and the buffer pointed by this pointer will contain valid data while std::string instance exists?
18
7843
by: Metro12 | last post by:
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::assign(_M_data(), _Rep::_S_terminal);
10
26654
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char* and I want to give it a std::string no matching function for call to `MD5::update(std::string&, size_t)' candidates are: void MD5::update(unsigned char*, unsigned int) void PrintMD5(string str){
5
9202
by: Tom Smith | last post by:
I hardly dare ask this given the furore in another thread over strings and const... My problem is this. I am assured that casting away the constness of the return value of std::string::c_str() is an Error according to the Standard. But I need to pass it to a function (in an old and unpleasant C library, ugh) which takes an ordinary char*. What should I do? Is it really necessary to make a fresh copy of the string? (I'm not asking this...
3
3703
by: Angus | last post by:
I can see how to get a char* but is it possible to get a wide char - eg wchar_t?
12
12453
by: sas | last post by:
hi, i need that because the path functions for windows, like PathAppend and PathRemoveFileExt accept a writable zero terminated char*, but i didn't find that for std::string, with CString, i usually use GetBuffer for that LPTSTR CString::GetBuffer( int nMinBufLength )
8
6321
by: puzzlecracker | last post by:
Any ideas what may cause that for the string class to core dump? #0 0x0018de92 in std::string::c_str () from /usr/lib/libstdc++.so.5
0
9718
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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,...
0
10613
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
10363
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
7649
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
6876
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
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.