By links77

2011-05-20 20:20:37 8 Comments

In a 2008 post on his site, Herb Sutter states the following:

There is an active proposal to tighten this up further in C++0x and require null-termination and possibly ban copy-on-write implementations, for concurrency-related reasons. Here’s the paper: . I think that one or both of the proposals in this paper is likely to be adopted, but we’ll see at the next meeting or two.

I know that C++11 now guarantees that the std::string contents get stored contiguously, but did they adopt the above in the final draft?

Will it now be safe to use something like &str[0]?


@James McNellis 2011-05-20 20:30:25

Yes. Per the C++0x FDIS, std::basic_string::c_str() must return

a pointer p such that p + i == &operator[](i) for each i in [0,size()].

This means that given a string s, the pointer returned by s.c_str() must be the same as the address of the initial character in the string (&s[0]).

@Xeo 2011-05-20 20:32:28

Note that the same requirement holds true for data, which I believe wasn't true for C++98/03.

@ildjarn 2011-05-20 20:35:11

Yes, it's illuminating that basic_string<>::c_str() and basic_string<>::data() now have exactly identical semantics.

@Andrew Marshall 2013-06-24 16:21:55

This doesn't appear to answer the question with which the post is titled - ie "Will std::string always be null-terminated in C++11?", in which case the answer is no. operator[str.length()] will return '\0', but that doesn't mean that the string actually contains it in memory.

@John Dibling 2013-07-18 13:16:59

I read this as well in the final C++11 spec (, but I don't see how any requirements are placed on the element at operator[str.length()], other than it must be valid a referenceable.

@John Dibling 2013-07-18 13:18:22

@AndrewMarshall: operator[] is required to return a reference to the actual stored element, so ( also applies the requirement that the element at operator[str.length()] must be part of the storage.

Related Questions

Sponsored Content

59 Answered Questions

[SOLVED] How do I read / convert an InputStream into a String in Java?

81 Answered Questions

[SOLVED] How do I make the first letter of a string uppercase in JavaScript?

47 Answered Questions

24 Answered Questions

[SOLVED] Case insensitive 'Contains(string)'

19 Answered Questions

[SOLVED] Why is char[] preferred over String for passwords?

17 Answered Questions

[SOLVED] Does Python have a string 'contains' substring method?

57 Answered Questions

[SOLVED] What is the difference between String and string in C#?

76 Answered Questions

[SOLVED] How do I iterate over the words of a string?

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2087984 View
  • 2781 Score
  • 76 Answer
  • Tags:   c++ string split

8 Answered Questions

[SOLVED] What is a lambda expression in C++11?

Sponsored Content