While you can safely read the right amount of data from the underlying storage, writing may not be a good idea, depending on the design.
vlad:Code ⧴ cat vectortest.cpp #include <vector> #include <iostream> int main() { using namespace std; vector<char> v(2); v.reserve(10); char c[6]={ 'H', 'e', 'l', 'l', 'o', '\0' }; cout << "Original size is " << v.size(); memcpy( v.data(), c, 6); cout << ", after memcpy it is " << v.size(); copy(c, c+6, v.begin()); cout << ", and after copy it is " << v.size() << endl; cout << "Arr: " << c << endl; cout << "Vec: "; for (auto i = v.begin(); i!=v.end(); ++i) cout << *i; cout << endl; } vlad:Code ⧴ make vectortest make: `vectortest' is up to date. vlad:Code ⧴ ./vectortest Original size is 2, after memcpy it is 2, and after copy it is 2 Arr: Hello Vec: He vlad:Code ⧴
So, if you write the message size() , then new data is not available using class methods.
You can explain this and make sure that the size is sufficient (for example, vector<char> v(10) ), but do you really want to make software where you work with the standard library?
source share