Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   vector: Foo[5] == ((foo*)Foo) + 5 ? (http://www.velocityreviews.com/forums/t636646-vector-foo-5-foo-foo-5-a.html)

 .rhavin grobert 09-23-2008 01:51 PM

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

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 09-23-2008 03:26 PM

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

On 23 Sep., 17:00, blargg....@gishpuppy.com (blargg) wrote:
> In article
>
> ".rhavin grobert" <cl...@yahoo.de> 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....

 Erik Wikström 09-23-2008 05:50 PM

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

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

 Andrey Tarasevich 09-23-2008 06:45 PM

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

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

 JaredGrubb 09-24-2008 01:57 PM

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

On Sep 23, 6:51*am, ".rhavin grobert" <cl...@yahoo.de> 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