|
-
June 28th, 2012, 10:50 AM
#20
Re: std::string and contiguous memory?
 Originally Posted by sszd
One question though, is data() guarenteed to return a null terminated string, or is that also implementation dependent? If not, this could also potentially cause a core dump by the library call.
data() does not guarantee null termination; c_str() guarantees null termination.
 Originally Posted by sszd
Does anyone have a quote from the standard that indicates std::string is implementation dependent and is not guaranteed to be in contiguous memory (prior to the C++11 standard of course)?
I think that this is by omission, i.e., since there was no requirement for storage to be contiguous, storage need not be contiguous. Herb Sutter has a word on this: Cringe not: Vectors are guaranteed to be contiguous (his reply concerning std::string on 2008-04-08 at 12:28 pm).
With respect to his statement that "current ISO C++ does require &str[0] to cough up a pointer to contiguous string data", I note that there was a defect in the standard in that C++03 defined operator[] as returning data()[pos] (or rather "as if" it did so), but of course this cannot be so for the non-const overload, so it leaves open to interpretation just what that might mean if an implementor wanted to push the boundaries.
 Originally Posted by sszd
Does data() return a char const* to memory that is independent of the memory where the actual string is stored, or is this also implementation dependent?
Implementation defined.
EDIT:
 Originally Posted by sszd
One question though, under the notes section it says... "Writing to the character array accessed through data is undefined behavior." Does this apply even to the C++11 standard even though the memory is guaranteed to be contiguous?
Yes. C++11 did not change that.
 Originally Posted by Codeplug
anything that doesn't require const_cast is the only correct solution.
I don't agree: with a legacy interface that is not const-correct, the use of const_cast can be valid. In this case, it wouldn't be.
Last edited by laserlight; June 28th, 2012 at 10:53 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|