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

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

1. ### .rhavin grobertGuest

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

2. ### .rhavin grobertGuest

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

3. ### Erik WikstrÃ¶mGuest

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
4. ### Andrey TarasevichGuest

..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>'.

--
Best regards,
Andrey Tarasevich
Andrey Tarasevich, Sep 23, 2008
5. ### JaredGrubbGuest

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