Can I call functions that take an array / pointer argument with std :: vector?

I want to rewrite code that uses many arrays unsigned char, use objects instead std::vector<unsigned char>. The problem lies in the fact that they are now used to store data to be written to the serial port or socket write buffer and to perform library functions require either void*or unsigned char*. An example of such a function is

  WriteToSocketBuffer(unsigned char* pBuffer, int iSize);

so currently i have a form code

 unsigned char* pArray = new unsigned char[iSize];
 //   populate array with data
 WriteToSocketBuffer(pArray,iSize);
 delete [] pArray;

My question is this: if I change my class to std::vector<unsigned char>instead of a raw array, I can just call my library function using

  std::vector<unsigned char> myVector;
  WriteToSocketBuffer(&myVector[0],myVector.size());

, . ?

+5
3

, , , .

:

++ 03 : [lib.vector] 23.2.4

......
, , v vector<T, Allocator>, T - , bool, &v[n] == &v[0] + n 0 <= n < v.size()

+12

++ 98 std::vector, .

++ 03, , , .

+6

Yes, you can do this until the buffer is empty.

If the vector is ever empty, then it &buffer[0]is an error, and it most likely will work, although the called function usually does not play the pointer because the size is 0.

+3
source

All Articles