184

I'm trying to use std::vector as a char array.

My function takes in a void pointer:

void process_data(const void *data);

Before I simply just used this code:

char something[] = "my data here";
process_data(something);

Which worked as expected.

But now I need the dynamicity of std::vector, so I tried this code instead:

vector<char> something;
*cut*
process_data(something);

The question is, how do I pass the char vector to my function so I can access the vector raw data (no matter which format it is – floats, etc.)?

I tried this:

process_data(&something);

And this:

process_data(&something.begin());

But it returned a pointer to gibberish data, and the latter gave warning: warning C4238: nonstandard extension used : class rvalue used as lvalue.

Chris Nolet
  • 8,714
  • 7
  • 67
  • 92
Rookie
  • 3,753
  • 5
  • 33
  • 33

3 Answers3

275

&something gives you the address of the std::vector object, not the address of the data it holds. &something.begin() gives you the address of the iterator returned by begin() (as the compiler warns, this is not technically allowed because something.begin() is an rvalue expression, so its address cannot be taken).

Assuming the container has at least one element in it, you need to get the address of the initial element of the container, which you can get via

  • &something[0] or &something.front() (the address of the element at index 0), or

  • &*something.begin() (the address of the element pointed to by the iterator returned by begin()).

In C++11, a new member function was added to std::vector: data(). This member function returns the address of the initial element in the container, just like &something.front(). The advantage of this member function is that it is okay to call it even if the container is empty.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 121
    **Important** Beware of `vector` which is the exception to this answer (and doesn't have a contiguous memory storage of `bool`s). – Motti Jun 26 '11 at 18:12
  • 20
    On the bright side, there's not much to beware of: all three of these methods will fail to compile with `std::vector` because `std::vector` requires the use of a proxy object and that proxy can't be implicitly converted to a `bool*`. As the workaround for this, if you do need a sequence of `bool`, it's best just to use a `std::vector`. @Motti – James McNellis Jun 26 '11 at 23:38
  • True, the _beware_ was in general and not directed as your answer, since there is no contiguous memory storage there is no way to get at it. – Motti Jun 27 '11 at 06:58
  • 7
    up for being comprehensive but mainly for `.data()` - I'lll just pretend I didn't see that ugly `&*iterator` :P – underscore_d Nov 17 '15 at 13:53
  • 2
    how long will the pointer returned from `data()` live? If the vector is never resized either larger or smaller (via `push_back()` or other functions, including `reserve`), is it guaranteed that the pointer will live as long as the vector lives, pointing to the correct place? – johnbakers Jul 11 '16 at 14:27
  • FYI, Scott Meyer's book "Effective STL" (for C++98) covers this topic in great depth in "Item 16: Know how to pass vector and string data to legacy APIs". – netjeff Oct 31 '16 at 18:27
  • Note that it is indeed safe to call data() on an empty vector, but its result is mostly useless. It will point to some memory, even though there are no elements in the vector which may be unexpected. – André Feb 15 '17 at 11:51
  • @johnbakers For your concerns, `std::vector::reserve` is the only way to make sure the pointers are valid even when adding items to the container. Otherwise, you will get dangling pointers. – Shaaer Apr 10 '23 at 21:01
95

something.data() will return a pointer to the data space of the vector.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
13

Take a pointer to the first element instead:

process_data (&something [0]);
Steven Don
  • 2,436
  • 15
  • 14