Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: What storage does std::string::c_str() use?

Reply
Thread Tools

Re: What storage does std::string::c_str() use?

 
 
vyacheslav@kononenko.net
Guest
Posts: n/a
 
      09-08-2004

Victor Bazarov wrote:
> 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 second call of c_str() changed the same buffer. If I
understood corectly standard does not say it is wrong, does it?
> Victor


Thanks,
Slava

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      09-08-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Victor Bazarov wrote:
>
>>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 second call of c_str() changed the same buffer. If I
> understood corectly standard does not say it is wrong, does it?


Yes, it does. By virtue of implication, the pointer returned by a call
to c_str() remains what the standard says it should be, _at_least_ until
a non-const member function for the string designated by 'this' is called.
IOW, if you do

char const * pc1 = foo.c_str();
char const * pc2 = bar.c_str();

you will get _two_different_pointers_ simply because you didn't call any
non-const members of std::basic_string for the object 'foo' before getting
the second pointer.

What implementation were you using that had the buffer shared? Not your
own, by any chance?

V
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      09-08-2004
(E-Mail Removed) wrote:
>
> Victor Bazarov wrote:
>> 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 second call of c_str() changed the same buffer. If I
> understood corectly standard does not say it is wrong, does it?


Where in the standard did you look. The language is as follows [21.3.6]:

const charT* c_str() const;
^^^^^^^^^^^^

*Returns:* A pointer to the initial element of an array of length size()+1
whose first size() elements equal the corresponding elements of the string
controlled by *this and whose last element is null character specified by
charT().

*Requires:* The program shall not alter any of the values stored in the
character array. Nor shall the program treat the returned value as a valid
pointer after any subsequent call to a non-const memer function of
basic_string that designates the same object as this.


Thus, modifying the buffer pointed to by c_str() is off limits.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Vyacheslav Kononenko
Guest
Posts: n/a
 
      09-09-2004
Victor Bazarov wrote:
> (E-Mail Removed) wrote:
>
>> Victor Bazarov wrote:
>>
>>> 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 second call of c_str() changed the same buffer. If I
>> understood corectly standard does not say it is wrong, does it?

>
>
> Yes, it does. By virtue of implication, the pointer returned by a call
> to c_str() remains what the standard says it should be, _at_least_ until
> a non-const member function for the string designated by 'this' is called.
> IOW, if you do
>
> char const * pc1 = foo.c_str();
> char const * pc2 = bar.c_str();
>
> you will get _two_different_pointers_ simply because you didn't call any
> non-const members of std::basic_string for the object 'foo' before getting
> the second pointer.
>
> What implementation were you using that had the buffer shared? Not your
> own, by any chance?
>
> V

Hehe if I remember correctly that was one in HP, but I am not sure.

Thanks,
Slava


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is enterprise storage so much more expensive than personal storage? John Computer Support 4 03-17-2008 09:50 PM
How to access the external storage unit of storage router =?Utf-8?B?SWduYXRpdXM=?= Wireless Networking 4 11-06-2006 06:40 AM
Difference b/w storage class and storage class specifier sarathy C Programming 2 07-17-2006 05:06 PM
What storage does std::string::c_str() use? Vyacheslav Kononenko C++ 2 09-09-2004 03:39 AM
Why does ISO affect storage space? Mike Harris Digital Photography 2 06-18-2004 01:58 AM



Advertisments