vector buf;
buf.reserve(N);
int M = read(fd, &buf[0], N);
This code fragment invokes undefined behavior. You can't write beyond than size()
elements, even if you have reserved the space.
The correct code is like:
vector buf;
buf.resize(N);
int M = read(fd, &buf[0], N);
buf.resize(M);
PS. Your statement "With vectors, one can assume that elements are stored contiguously in memory, allowing the range
[&vec[0], &vec[vec.capacity())
to be used as a normal array" isn't true. The allowable range is
[&vec[0], &vec[vec.size())
.