CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 45

Threaded View

  1. #20
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: std::string and contiguous memory?

    Quote 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.

    Quote 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.

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

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured