Improve company productivity with a Business Account.Sign Up


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

Posted on 2004-04-14
Medium Priority
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
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 2000 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?

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.


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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

606 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