[ACCEPTED]-Converting (void*) to std::vector<unsigned char>-types

Accepted answer
Score: 29

You will need the length of the buffer. Once 18 you do, we can do this:

unsigned char *charBuf = (unsigned char*)voidBuf;
/* create a vector by copying out the contents of charBuf */
std::vector<unsigned char> v(charBuf, charBuf + len);

Okay, the comment 17 got me started on why I did not use reinterpret_cast:

  • In 16 C++, the C-style cast is a convenience function 15 -- it asks the compiler to choose the safest 14 and most portable form of conversion over 13 the set of available cast operators.

  • The 12 reinterpret_cast is implementation defined and should always 11 be the last thing on your mind (and used 10 when you are necessarily doing a non-portable 9 thing knowingly).

  • The conversion between 8 (unsigned doesn't change the type) char * and void * is portable 7 (you could actually use static_cast if you are really 6 picky).

The problem with the C-style cast 5 is: the added flexibility can cause heartaches 4 when the pointer type changes.

Note: I agree with 3 the general convention of not casting as 2 much as possible. However, without any source 1 provided, this is the best I could do.

Score: 3

You can't simply cast a void* to a std::vector<unsigned char> because the 6 memory layout of the latter includes other 5 objects, such as the size and the number 4 of bytes currently allocated.

Assuming the 3 buffer is pointed to by buf and its length 2 is n:

vector<unsigned char> vuc(static_cast<char*>(buf), static_cast<char*>(buf) + n);

will create a copy of the buffer that you 1 can safely use.

[EDIT: Added static_cast<char*>, which is needed for pointer arithmetic.]

Score: 3

The only time this would be legitimate is 9 if you had already created a vector, and 8 simply wanted to get it back.

void SomeFunc(void* input);

main() {
  std::vector< unsigned char > v;
  SomeFunc((void*) &v);

SomeFunc(void* input) {
  // Now, you could cast that void* into a vector
  std::vector< unsigned char >* v_ = (vector<unsigned char>*)input

I haven't actually 7 tried to see if this will run, but that's 6 the spirit of it. That said, if you are 5 making this from scratch, you are definitely 4 doing it wrong. This is really bad. The 3 only time this could be even remotely understandable 2 is if you are forced to implement the already 1 defined "SomeFunc()".

Score: 2

using std::vector class for an already allocated 12 buffer is not a solution. A std::vector 11 object manages the memory and deallocates 10 it at destruction time.

A complicated solution 9 might be to write your own allocator, that 8 uses an already allocated buffer, but you 7 have to be very careful on several scenarios, like 6 vector resizing, etc.

If you have that void* buffer 5 bound through some C API functions, then 4 you can forget about conversion to std::vector.

If 3 you need only a copy of that buffer, it 2 can be done like this:

std::vector< unsigned char> cpy( 
    (unsigned char*)buffer, (unsigned char*)buffer + bufferSize);

where bufferSize is 1 the size in chars of the copied buffer.

More Related questions