Solved

When references, pointers, and iterators to a string become invalid

Posted on 2004-04-14
6
251 Views
Last Modified: 2010-04-01
From the standard I gather that references, pointers, and iterators to a string become invalid after:

calling any non-const member functions (with the exception of [], at, begin, end, rbegin, rend)

calling data or c_str

the first call to non-const functions [], at, begin, end, rbegin, rend

However, what isn't immediately obvious is that the standard defines compare() as:

traits::compare(data(), str.data(), rlen);

And most string implementations use compare() in the comparison operators.

So can one add to that list all of the compare() overloads and comparison operators?

Is that a justifiable statement? And might there be any more exceptions?

Thanks,
-Sandra
0
Comment
Question by:Sandra-24
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 10829240
>>So can one add to that list all of the compare() overloads and comparison operators?

Give me a reason why a comparison is not "const"  regarding the objects being compared and I will say "no" - I guess it is a "yes", though :o)

In other words: Do not rely on the 'const' keyword alone. But, saying that, I start to wonder why there are no 3 'const' overloads for all 4 combinations of comparison...
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 10829251
>>Is that a justifiable statement?

Forgot to write "yes".
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10830822
Hi Sandra-24,
> calling data or c_str

Why should calling c_str invalidate a pointer or reference? Or even an iterator?

Cheers,
Stefan
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 9

Expert Comment

by:jasonclarke
ID: 10840909
> Why should calling c_str invalidate a pointer or reference?

Realistically it probably will not invalidate the pointer.

However, the standard allows for the fact that the string could be implemented such that an internal representation may not occupy contiguous storage - e.g. while building a string it could store different sections of the string in multiple places.

However, when the buffer returned by c_str/data must be contigous, so in such an implementation, at this point the class would have to reallocate some contiguous space for the string.
0
 
LVL 3

Author Comment

by:Sandra-24
ID: 10846898
Comparison operations SHOULD be const. Any string implementation with a data() function that invalidates pointers,references, or iterators SHOULD not call data() in their comparison operators as the standard seems to hint. However, as far as the standard is concerned, a string implementation could behave this way. Realisticaly it's probably only a concern to implementors of STL strings. I would have to think that any place where data() must be mutating in a way that invalidates references (and I really can't think of one!) that the programmers would have been clever enough to design a compare function that is non-mutating (i.e. doesn't use data() internally). However, the good folks that created the standard must have envisioned a possible scenario (I would hope they didn't just add the rule for fun) where data() could invalidate references, pointers and iterators.
0
 
LVL 9

Expert Comment

by:jasonclarke
ID: 10857656
>  However, the good folks that created the standard must have envisioned a possible scenario

There is quite a lot of evidence that they messed up in the design of the string class (e.g. see this reference: http://www.gotw.ca/gotw/084.htm ).

I think that were they starting again,  std::string might be more prescriptive in design, but would be a lot simpler...  This is the kind of thing that C++ detractors can point to as evidence of how pointlessly complicated the language is - this is only a string, after all.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question