Suppose you have a vector of
and function that takes
. To obtain the address of the internal array of the vector v and pass it to the function, use the expressions
void func(const int arr, size_t length );
vector <int> vi;
It is safe to use
as the internal array’s address as long as you adhere to the following rules:
func() shouldnot access out-of-range array elements.
- the elements inside the vector must be contiguous. Although the C++ Standard doesnot guarantee that yet
However,I amn't aware of any implementation that doesn’t use contiguous memory for vectors. Furthermore, this loophole in the C++ Standard will be fixed soon.