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

Posted on 2004-04-14
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(),, 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?

Question by:Sandra-24
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 86

Expert Comment

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...
LVL 86

Accepted Solution

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

Forgot to write "yes".
LVL 12

Expert Comment

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

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.

Author Comment

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.

Expert Comment

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: ).

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.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

696 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