1

I have a binary file that I'm reading from. In the file at one point, there is a known number of bytes which make up a simple ASCII string (possibly with newlines). Right now I have:

void doSomething(istream &in) {
    vector<char> buf(length + 1, '\0');
    in.read(&buf[0], length);
    string str(&buf[0]);
}

But I realized that it would probably be faster to do

void doSomething(istream &in) {
    string str(length + 1, '\0'); // +1 needed?
    in.read(&str[0], length);
}

I tried looking in the standard to see if string allocation is always sequential or if it's safe doing something like this. Safe meaning, no accidental reading into (writing to) memory not part of the string. Does anyone know?

bombax
  • 1,189
  • 8
  • 26

1 Answers1

2

std::string allocation is always sequential, at least as of C++11. I believe prior to that it wasn't clearly defined so, but no implementations used non-sequential storage.

You do not need to explicitly add space for a null terminator in std::string.

bames53
  • 86,085
  • 15
  • 179
  • 244
  • Thanks! Do you also happen to know if its necessary to include a null-terminator when reading like this, or is it implementation dependent? – bombax Jun 09 '15 at 21:17
  • Reading the duplicate question at stackoverflow.com/q/25169915/241631 the answer says "[...] and your code avoids overwriting the terminating NULL character [...]" suggesting it is necessary to include a null character? Or did I misunderstand? – bombax Jun 09 '15 at 21:31
  • 1
    @bombax You can write to any index in the range [0, `str.size()`), and `str[str.size() - 1]` does not have to be `'\0'`. – bames53 Jun 09 '15 at 21:37
  • That's perfectly clear, thanks! – bombax Jun 09 '15 at 21:38