Get TYPE* array from vector<TYPE> - is this OK?

Discussion in 'C++' started by Goran Pusic, Sep 7, 2010.

  1. Goran Pusic

    Goran Pusic Guest

    Hi all!

    Given:
    std::vector<TYPE> v;
    void process(size_t count, TYPE* data);

    Is there any problem if I do

    if (!v.empty())
    {
    std::vector<TYPE>::reference r = *v.begin();
    // or
    r = v[0];
    // or
    r = v.at(0);
    process_raw(v.size(), &p);
    }

    ?

    (This is what I do all the time to interface with lower-level code,
    but I've always been wondering if there's some issue in doing things
    like these).

    TIA,

    Goran.
    Goran Pusic, Sep 7, 2010
    #1
    1. Advertising

  2. On Sep 7, 3:26 pm, Goran Pusic <> wrote:
    > Hi all!
    >
    > Given:
    > std::vector<TYPE> v;
    > void process(size_t count, TYPE* data);
    >
    > Is there any problem if I do
    >
    > if (!v.empty())
    > {
    >   std::vector<TYPE>::reference r = *v.begin();
    >   // or
    >   r = v[0];
    >   // or
    >   r = v.at(0);
    >   process_raw(v.size(), &p);
    >
    > }
    >
    > ?


    No problem at all. It was for supporting code like this that
    std::vector always has had the guarantee that it uses contiguous
    storage for it elements.

    Bart v Ingen Schenau
    Bart van Ingen Schenau, Sep 7, 2010
    #2
    1. Advertising

  3. * Bart van Ingen Schenau, on 07.09.2010 15:42:
    > On Sep 7, 3:26 pm, Goran Pusic<> wrote:
    >> Hi all!
    >>
    >> Given:
    >> std::vector<TYPE> v;
    >> void process(size_t count, TYPE* data);
    >>
    >> Is there any problem if I do
    >>
    >> if (!v.empty())
    >> {
    >> std::vector<TYPE>::reference r = *v.begin();
    >> // or
    >> r = v[0];
    >> // or
    >> r = v.at(0);
    >> process_raw(v.size(),&p);
    >>
    >> }
    >>
    >> ?

    >
    > No problem at all. It was for supporting code like this that
    > std::vector always has had the guarantee that it uses contiguous
    > storage for it elements.


    Not "always"; it got that guarantee in C++03.


    Cheers,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 7, 2010
    #3
  4. * Marcel Müller, on 07.09.2010 15:49:
    > Goran Pusic wrote:
    >> Hi all!
    >>
    >> Given:
    >> std::vector<TYPE> v;
    >> void process(size_t count, TYPE* data);
    >>
    >> Is there any problem if I do
    >>
    >> if (!v.empty())
    >> {
    >> std::vector<TYPE>::reference r = *v.begin();
    >> // or
    >> r = v[0];
    >> // or
    >> r = v.at(0);
    >> process_raw(v.size(), &p);

    >
    > p is undefined. You probably mean
    > process_raw(v.size(), &*v.begin());


    process_raw is undefined. You probably mean

    process( v.size, &r );


    >> }

    >
    > I remember that we had the same discussion some time ago. AFAIR the result was
    > that the storage layout of vector is not guaranteed by the standard in this way,
    > but this is more an accident rather then intensionally.


    A std::vector is guaranteed to have contiguous storage, per C++03.


    Cheers & hth.,

    - Alf

    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach /Usenet, Sep 7, 2010
    #4
  5. Goran Pusic wrote:
    >
    > Given:
    > std::vector<TYPE> v;
    > void process(size_t count, TYPE* data);
    >
    > Is there any problem if I do
    >
    > if (!v.empty())
    > {
    > std::vector<TYPE>::reference r = *v.begin();
    > // or
    > r = v[0];
    > // or
    > r = v.at(0);
    > process_raw(v.size(), &p);
    > }
    >
    > ?


    Yes, the technique it is perfectly valid.

    You can simply use `&v.front()` to obtain the pointer to the first
    element of the raw array, instead of going through the iterators and all
    these intermediate dereferences and address-takings.

    Also, be aware of specialized versions of `std::vector<>`, like
    `std::vector<bool>` for example, which will not work with this technique
    (it stores bool values in a packed array of bits, providing no direct
    access to the raw array).


    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Sep 8, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. pmatos
    Replies:
    6
    Views:
    23,778
  2. Replies:
    8
    Views:
    1,913
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  4. Thomas
    Replies:
    7
    Views:
    313
    Mark Hubbart
    May 23, 2005
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    356
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page