How to access std :: vector's inner contiguous buffer and use it with memcpy etc.?

How can I access the contiguous memory buffer used in std :: vector so that I can perform direct memory operations on it (e.g. memcpy)? Also, is it safe to perform operations like memcpy in this buffer?

I read that the standard ensures that the vector uses the internal memory buffer inside, but it is not necessarily implemented as a dynamic array. I believe that it is definitely contiguous, I should be able to use it as such, but I was not sure that the implementation of the vector stores accounting data as part of this buffer. If this happened, something like memcpying a vector buffer would destroy its internal state.

+4
source share
7 answers

In practice, almost all compilers implement vector as an array under the hood. You can get a pointer to this array by executing &somevector[0] . If the contents of the vector are POD types ('plain-old-data'), then memcpy should be safe — however, if they are C ++ classes with complex initialization logic, you can be more secure with stand :: copy .

+12
source

Just do

 &vec[0]; // or Goz suggestion: &vec.front(); // or &*vec.begin(); // but I don't know why you'd want to do that 

This returns the address of the first element in the vector (assuming vec has more than 0 elements), which is the address of the array used. vector storage is guaranteed to be standard to be contiguous, so this is a safe way to use a vector with functions that arrays expect.

Remember that if you add or remove elements from a vector or [potentially] modify the vector in any way (for example, call reserve ), this pointer may become invalid and point to the freed memory area.

+9
source

You can simply do:

  &vect[0] 

Memory is guaranteed to be continuous, so it is safe to work with it using C library functions such as memcpy. However, you should not persist in pointers in related data, since vector changes can redistribute and copy memory to another location. IE would be bad:

  std::vector<char> charVect; // insert a bunch of stuff into charVect ... char* bufferPtr = &charVect[0]; charVect.push_back('a'); // potential resize // Now bufferPtr may not be valid since the resize may have moved // the vectors contents bufferPtr[0] = 'f'; // **CRASH** 
+6
source
 &myvec[0] 

But note that using memcpy really only applicable if it is a vector of POD or primitive types. Performing direct manipulation of the memory of any other leads to undefined behavior.

+2
source

The easiest way is to use &v[0] , where v is your vector. Example:

 int write_vector(int fd, const std::vector<char>& v) { int rval = write(fd, &v[0], v.size()); return rval; } 
+2
source

Yes - since the standard guarantees the continuous placement of internal vector data, you can access the pointer to the first element in the vector through:

 std::vector<int> my_vector; // initialize... int* arr = &my_vector[0]; 
+1
source

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?

0
source

All Articles