vector: Foo[5] == ((foo*)Foo) + 5 ?

Discussion in 'C++' started by .rhavin grobert, Sep 23, 2008.

  1. assume the following:
    ______________________
    std::vector<foo> m_vFoo; // we assume foo has more than four
    elements!

    foo* FifthElement(foo* pFooZero)
    // we call +0 the zeroth element!
    {
    return pFooZero + 5;
    };
    ______________________

    is &m_vFoo[5] the same as FifthElement(m_vFoo) ? Allways?
    .rhavin grobert, Sep 23, 2008
    #1
    1. Advertising

  2. On 23 Sep., 17:00, (blargg) wrote:
    > In article
    > <>,
    >
    > ".rhavin grobert" <> wrote:
    > > foo* FifthElement(foo* pFooZero)
    > > // we call +0 the zeroth element!
    > > {
    > >   return pFooZero + 5;
    > > };
    > > ______________________

    >
    > Don't you mean either SixthElement, or an index of 4? Remember, in C++, 0
    > is the FIRST element.


    little typo.

    1 is the FIRST element!
    0 is the ZEROTH element!

    but that's esotheric....
    .rhavin grobert, Sep 23, 2008
    #2
    1. Advertising

  3. On 2008-09-23 15:51, .rhavin grobert wrote:
    > assume the following:
    > ______________________
    > std::vector<foo> m_vFoo; // we assume foo has more than four
    > elements!
    >
    > foo* FifthElement(foo* pFooZero)
    > // we call +0 the zeroth element!
    > {
    > return pFooZero + 5;
    > };
    > ______________________
    >
    > is &m_vFoo[5] the same as FifthElement(m_vFoo) ? Allways?


    If I understand you correctly what you are asking is if the address of
    the vector is also the address of its first element, and since the
    vectors elements are stored in contiguous memory if the address of the
    vector + sizeof(foo) * 5 is also the address of the sixth (element with
    index 5) element.

    The answer is with hight probability no, the vector contains a pointer
    to the first element which will be located elsewhere.

    --
    Erik Wikström
    Erik Wikström, Sep 23, 2008
    #3
  4. ..rhavin grobert wrote:
    > assume the following:
    > ______________________
    > std::vector<foo> m_vFoo; // we assume foo has more than four
    > elements!
    >
    > foo* FifthElement(foo* pFooZero)
    > // we call +0 the zeroth element!
    > {
    > return pFooZero + 5;
    > };
    > ______________________
    >
    > is &m_vFoo[5] the same as FifthElement(m_vFoo) ? Allways?
    >


    Never. You question makes no sense since neither

    FifthElement(m_vFoo)

    nor

    (foo*)Foo

    is a valid expression in C++ for 'm_vFoo'/'Foo' of type 'std::vector<foo>'.

    Clarify your question.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Sep 23, 2008
    #4
  5. .rhavin grobert

    JaredGrubb Guest

    On Sep 23, 6:51 am, ".rhavin grobert" <> wrote:
    > assume the following:
    > ______________________
    > std::vector<foo> m_vFoo;  // we assume foo has more than four
    > elements!
    >
    > foo* FifthElement(foo* pFooZero)
    > // we call +0 the zeroth element!
    > {
    >   return pFooZero + 5;};
    >
    > ______________________
    >
    > is &m_vFoo[5] the same as FifthElement(m_vFoo) ? Allways?


    Not exactly. m_vFoo is not convertible to foo*, so your code wont
    compile. However, the following does work and is true:

    &m_vFoo[5] == FifthElement(&*m_vFoo.begin())

    This is actually used in a container benchmark written by Alexander
    Stepanov to test whether a compiler adds any overhead to iterators,
    when in theory a vector iterator should be a thin wrapper for a foo*.
    (http://www.stepanovpapers.com/container_benchmark.cpp)
    JaredGrubb, Sep 24, 2008
    #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,698
  2. Replies:
    3
    Views:
    368
    John Roth
    Jul 29, 2005
  3. Joseph Turian
    Replies:
    8
    Views:
    404
    Earl Purple
    Jan 27, 2006
  4. Replies:
    8
    Views:
    1,873
    Csaba
    Feb 18, 2006
  5. Replies:
    4
    Views:
    124
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page