0

I cannot understand if char * returned from string.c_str() points to the same buffer (and not making any copy) how is it null terminated? does the function add null-terminator in the end of string? and what happens if buffer doesn't have any extra bytes to store it?

thanks.

JHBonarius
  • 10,824
  • 3
  • 22
  • 41
  • C++ strings are always null-terminated, cf. http://eel.is/c++draft/string.classes#basic.string-4 – Henri Menke Jun 13 '18 at 09:43
  • 1
    The buffer *has* an extra byte to hold the terminator, always. In theory it could store the terminator value only when you ask to see it, but in practice it is always there. – Bo Persson Jun 13 '18 at 09:49
  • if null terminator is always there it means that string.data() is EXACTLY the same as string.c_str()? – shota silagadze Jun 13 '18 at 11:40

1 Answers1

1

Since C++11, strings are null-terminated internally, and both c_str() and data() return the same thing.

VLL
  • 9,634
  • 1
  • 29
  • 54