C++ Tip : Treating a Vector as an Array

Started by
10 comments, last by jpetrie 13 years, 9 months ago
Suppose you have a vector of int and function that takes int *. To obtain the address of the internal array of the vector v and pass it to the function, use the expressions &v[0] or &*v.front(). For example:
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]
Advertisement
It would be &v.front(), not &*v.front().

Are you sure that the standard doesn't specify that the storage will be contiguous? (I was under the impression that it did...)
The original version of the C++ standard did not require vector to store objects in contiguous memory. This was fixed in TC1. Here's the defect report. In fact I'm pretty sure that this entire post is (probably indirectly) a rip from something written by Sutter, Meyers or another of that crowd. Also note that you must also guarantee that the vector is not empty before applying this technique.
EliteHussar: Apart from what the others said about continuousness of std::vector's storage, which as said *is* guaranteed, the thing about taking the address of the first element is a pretty standard way of dealing with functions that take arrays or pointers.

Quote:Furthermore, this loophole in the C++ Standard will be fixed soon.

As said, it was fixed long ago. But how exactly did you build up your knowledge and those conjectures?

Btw, for up-to-date looks into the standard, just go to the primary source http://open-std.org. The C++ drafts are free as in free beer.
Guys, this is just a spam post trying to drive traffic to that site...
Quote:Original post by Hodgman
Guys, this is just a spam post trying to drive traffic to that site...


Really? Teh fuk. I just posted a reply there ... my senses are getting older
Quote:Guys, this is just a spam post trying to drive traffic to that site...
Crap - can't believe I fell for that :\
Quote:Original post by Enigma
In fact I'm pretty sure that this entire post is (probably indirectly) a rip from something written by Sutter, Meyers or another of that crowd.
Almost, but not quite. Still, good eye :-)

A quick copyscape over the site shows that 90% of it is a word by word rip from devx.com (and the remaining 10% are from other webpages, including this one).
This is so fuked. I've signed on a damned copycat's impostor's crap spam site with my fuking real nickname. Ima getting so old these days (or it's simply too hot here on average)
Lots of spam.

This topic is closed to new replies.

Advertisement