5

I want to rewrite some code that uses a lot of unsigned char arrays, to instead make use of std::vector<unsigned char> objects. The problem I have is that these are currently used to store the data that will be written to either a serial port or socket write buffer and the library functions to do this take either void* or unsigned char* . An example of such a function is

  WriteToSocketBuffer(unsigned char* pBuffer, int iSize);

so currently I have code of the form

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

My question is the following: If I change my class to have a std::vector<unsigned char> instead of a raw array can I simply call my library function using

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

Does passing the address of the first element in the vector act in the same was as passing in the address of the first element in a raw array. Is it this simple?

5
  • In C++11 you can also use std::array. Commented Aug 21, 2012 at 16:04
  • 3
    @DragoonWraith, how trying would help? Things like allowing non contiguous representation may break it in general and yet succeed in basic tests. Commented Aug 21, 2012 at 16:05
  • @DragoonWraith Rather than rewrite core functionality that will take a week and massively impact on my code internals, I thought I would ask the question first just to check that what I suspect will happen actually will happen. Commented Aug 21, 2012 at 16:08
  • Note that as-written this is undefined behavior, because myVector contains no elements. You want std::vector<unsigned char> myVector(iSize); instead. Commented Aug 21, 2012 at 18:41
  • @GManNickG Yes indeed, I omitted the populating of the vector. I was really just trying to convey the general idea. I should have inserted a commented code line for clarity. Commented Aug 21, 2012 at 19:24

3 Answers 3

12

Yes, The elements of a vector are assured to be contiguous similar to an array.

Reference:

C++03 Standard: [lib.vector] 23.2.4 Class template vector

......
The elements of a vector are stored contiguously, meaning that if v is a vector<T, Allocator> where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()

Sign up to request clarification or add additional context in comments.

1 Comment

I thought it would probably be OK but thanks for confirming it.
6

C++98 didn't mandate contiguous allocation for the data in an std::vector, but that's what all known implementations did anyway.

As of C++03, that was standardized as a requirement, so it's now required, not just how things happen to be.

Comments

2

Yes, you can do that, as long as the buffer is never empty.

If the vector is ever empty, then &buffer[0] is an error, and it will likely crash even though the called function wouldn't normally dereference the pointer because the size is 0.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.