Jump to content
  • Advertisement
Sign in to follow this  
EliteHussar

C++ Tip : Treating a Vector as an Array

This topic is 3012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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...)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Quote:
Guys, this is just a spam post trying to drive traffic to that site...
Crap - can't believe I fell for that :\

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites

This topic is 3012 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!