void func(const int arr[], size_t length ); int main() { vector <int> vi; //.. fill vi func(&vi[0], vi.size()); }
It’s safe to use &vi[0] and &*v.front() as the internal array’s address as long as you adhere to the following rules: First, func() shouldn’t access out-of-range array elements. Second, the elements inside the vector must be contiguous. Although the C++ Standard doesn’t guarantee that yet, I’m not aware of any implementation that doesn’t use contiguous memory for vectors. Furthermore, this loophole in the C++ Standard will be fixed soon.
MOD EDIT: URLs removed.
[Edited by - Evil Steve on July 22, 2010 3:39:55 AM]