Read N bytes from the file and add them to std :: vector

I want to read N bytes of data from a file stream and add them to a vector. So let's say we have

 basic_ifstream<uint8_t> myFileStream; 

and a

 vector<uint8_t> myBuffer; 

I am currently doing something like this:

 myBuffer.reserve(N); for (int i=0; i<N; ++i) { uint8_t tmpByte; myFileStream.read(&tmpByte, 1); myBuffer.push_back(tmpByte); } 

but it is very slow.

Now I tried to let myFileStream.read copy the data directly to the vector. Since the vector stores its elements in adjacent storage, I thought something like this should be possible:

 uint8_t* ptr = &myBuffer.back(); // there is already some elements in the buffer (I know) ptr++; // first element after existing data myBuffer.resize(myBuffer.size() + N); myFileStream.read(ptr, N); 

But with this, I get a runtime error (heap corruption). What is wrong with this decision? Or is there a better way to do this anyway?

+6
source share
1 answer

Your problem is that resize may need to redistribute the entire vector and thus invalidate the previous ptr . You need to take the pointer only after resize .

 std::size_t oldSize = myBuffer.size(); // resize first myBuffer.resize(oldSize + N); uint8_t* ptr = &myBuffer[oldSize]; // already first element after existing data myFileStream.read(ptr, N); 

Note that as a bonus, this implementation will work even if the original vector is empty (for N != 0 , of course).

+12
source

Source: https://habr.com/ru/post/896956/


All Articles