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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • .rhavin grobert

    #1

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

    assume the following:
    _______________ _______
    std::vector<foo m_vFoo; // we assume foo has more than four
    elements!

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

    is &m_vFoo[5] the same as FifthElement(m_ vFoo) ? Allways?


  • Victor Bazarov

    #2
    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(fo o* pFooZero)
    // we call +0 the zeroth element!
    {
    return pFooZero + 5;
    };
    _______________ _______
    >
    is &m_vFoo[5] the same as FifthElement(m_ vFoo) ? Allways?
    I think you meant "m_vFoo has more than five elements". To make the
    index '5' valid, *the vector* has to have *at least* 6 elements. And it
    has nothing really to do with the 'foo' type itself.

    There is no conversion from a vector<footo a foo*. So, no the code is
    not valid.

    What you *could* state is:

    &m_vFoo[5] == FifthElement(&m _vFoo[0])

    That is guaranteed by the Standard for all 'foo' except 'bool'. The
    storage vector allocates is contiguous.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask

    Comment

    • blargg

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

      In article
      <cb448cf7-681d-460b-894b-7c5d39bc5b98@e5 3g2000hsa.googl egroups.com>,
      ".rhavin grobert" <clqrq@yahoo.de wrote:
      foo* FifthElement(fo o* 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.

      Comment

      • .rhavin grobert

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

        On 23 Sep., 17:00, blargg....@gish puppy.com (blargg) wrote:
        In article
        <cb448cf7-681d-460b-894b-7c5d39bc5...@e5 3g2000hsa.googl egroups.com>,
        >
        ".rhavin grobert" <cl...@yahoo.de wrote:
        foo* FifthElement(fo o* 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....

        Comment

        • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

          #5
          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(fo o* 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

          Comment

          • Andrey Tarasevich

            #6
            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(fo o* 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<fo o>'.

            Clarify your question.

            --
            Best regards,
            Andrey Tarasevich

            Comment

            • JaredGrubb

              #7
              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(fo o* 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)

              Comment

              Working...