Solved

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

Posted on 2004-04-14
6
229 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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 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 pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now