You are on page 1of 1

11/18/11

InformIT: 30 C++ Tips in 30 Days: Tip # 7 Accessing a vector's Internal buff


Other Network Sites

Account Sign In

View Your Cart

The Trusted Technology Learning Source

Become an InformIT Member


Take advantage of special member promotions, everyday discounts, quick access to saved content, and more! Join Today.

Home > Blogs > 30 C++ Tips in 30 Days: Tip # 7 Accessing a vector's Internal buffer portably

30 C++ Tips in 30 Days: Tip # 7 Accessing a vector's Internal buffer portably


Danny Kalev Posted November 25, 2007 Topics: Programming, C/C++

Other Things You Might Like


Service-Oriented Infrastructure: OnPrem ise and in the Cloud By Thomas Erl, Mark Little, Arnaud Simon, Thomas Rischbeck, Anthony Assi, David Chappell, John deVadoss, Francois Lascelles, Anna Liu, Tom Plunkett, Satadru Roy, Herbjrn Wilhelmsen $47.99 Introduction to Program m ing Using Python By Y. Daniel Liang $91.80 Experiences of Test Autom ation: Case Studies of Softw are Test Autom ation By Dorothy Graham, Mark Few ster $31.99

Most of the time, you won t need to access the underlying data buffer of a vector. However, there are exceptional circumstances under which accessing the raw memory buffer is needed. Here's how you can do it portably. Suppose you have a vector of int and function that takes int *. To obtain the address of the internal array of the vector vec use the expressions &v[0] or &*v.front(). For example: void f(const int arr[], std::size_t len); int main() { vector <int> vec; //.. fill vec f(&vi[0], vi.size()); } The C++03 Standard guarantees that vector elements shall occupy contiguous memory so the address of the first element is also the address of the entire array. Notice that some implementations also offer a vector::data() member function which returns the address of the internal vector buffer. However, data() isn't portable.

Share |

Comments
Like

Showing 0 comments
Sort by Oldest first Subscribe by email Subscribe by RSS

Add New Comment

Image
comments powered by DISQUS

Post as

www.informit.com/blogs/blog.aspx?uk=30-C-Tips-in-30-Days-Tip-7-Accessi

1/2

You might also like