"++*this" crashing

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

    "++*this" crashing

    hi,
    i am incrementing a list iterator "m_item" in my .cpp file
    m_item++
    m_item._ptr->_next is showing the valid item.

    but in the library overloaded function for ++

    1 >_Myt_iter operator++(int)
    2 > { // postincrement
    3 > _Myt_iter _Tmp = *this;
    4 > ++*this;
    5 > return (_Tmp);
    6 > }

    (line 4)++*this is crashing...it is not showing any item in its next
    pointer.

    what is the problem?

    thanks,
    srinivas

  • Ondra Holub

    #2
    Re: "++*this&q uot; crashing


    srinivas napsal:
    hi,
    i am incrementing a list iterator "m_item" in my .cpp file
    >
    m_item++
    >
    m_item._ptr->_next is showing the valid item.
    >
    but in the library overloaded function for ++
    >
    1 >_Myt_iter operator++(int)
    2 > { // postincrement
    3 > _Myt_iter _Tmp = *this;
    4 > ++*this;
    5 > return (_Tmp);
    6 > }
    >
    (line 4)++*this is crashing...it is not showing any item in its next
    pointer.
    >
    what is the problem?
    >
    thanks,
    srinivas
    You should post here minimized compilable example which crashes.
    Without it we can only guess.

    Comment

    • srinivas

      #3
      Re: "++*this&q uot; crashing

      hi Ondra
      thanks for reply.

      is assignment of an iterator to another iterator valid?
      i have found this assignment is creating problem.
      >m_iter_item = iter_item;
      >m_iter_item+ +;
      srinivas

      Ondra Holub wrote:
      srinivas napsal:
      hi,
      i am incrementing a list iterator "m_item" in my .cpp file
      m_item++
      m_item._ptr->_next is showing the valid item.

      but in the library overloaded function for ++

      1 >_Myt_iter operator++(int)
      2 > { // postincrement
      3 > _Myt_iter _Tmp = *this;
      4 > ++*this;
      5 > return (_Tmp);
      6 > }

      (line 4)++*this is crashing...it is not showing any item in its next
      pointer.

      what is the problem?

      thanks,
      srinivas
      >
      You should post here minimized compilable example which crashes.
      Without it we can only guess.

      Comment

      • Ondra Holub

        #4
        Re: "++*this&q uot; crashing


        srinivas napsal:
        hi Ondra
        thanks for reply.
        >
        is assignment of an iterator to another iterator valid?
        i have found this assignment is creating problem.
        >
        m_iter_item = iter_item;
        m_iter_item++;
        In general, it is valid. I think you have incorrect operator=. I do not
        know internal structures of your iterator, so I am only guessing.
        >
        srinivas
        >
        Ondra Holub wrote:
        srinivas napsal:
        hi,
        i am incrementing a list iterator "m_item" in my .cpp file
        >
        m_item++
        >
        m_item._ptr->_next is showing the valid item.
        >
        but in the library overloaded function for ++
        >
        1 >_Myt_iter operator++(int)
        2 > { // postincrement
        3 > _Myt_iter _Tmp = *this;
        4 > ++*this;
        5 > return (_Tmp);
        6 > }
        >
        (line 4)++*this is crashing...it is not showing any item in its next
        pointer.
        >
        what is the problem?
        >
        thanks,
        srinivas
        You should post here minimized compilable example which crashes.
        Without it we can only guess.

        Comment

        • srinivas

          #5
          Re: "++*this&q uot; crashing

          but the assignment operator= is not overloaded for list iterators.
          so i am thinking assignment has gone wrong.

          Ondra Holub wrote:
          srinivas napsal:
          hi Ondra
          thanks for reply.

          is assignment of an iterator to another iterator valid?
          i have found this assignment is creating problem.
          >m_iter_item = iter_item;
          >m_iter_item+ +;
          >
          In general, it is valid. I think you have incorrect operator=. I do not
          know internal structures of your iterator, so I am only guessing.
          >

          srinivas

          Ondra Holub wrote:
          srinivas napsal:
          hi,
          i am incrementing a list iterator "m_item" in my .cpp file

          m_item++

          m_item._ptr->_next is showing the valid item.

          but in the library overloaded function for ++

          1 >_Myt_iter operator++(int)
          2 > { // postincrement
          3 > _Myt_iter _Tmp = *this;
          4 > ++*this;
          5 > return (_Tmp);
          6 > }

          (line 4)++*this is crashing...it is not showing any item in its next
          pointer.

          what is the problem?

          thanks,
          srinivas
          >
          You should post here minimized compilable example which crashes.
          Without it we can only guess.

          Comment

          • Ondra Holub

            #6
            Re: "++*this&q uot; crashing


            srinivas napsal:
            but the assignment operator= is not overloaded for list iterators.
            so i am thinking assignment has gone wrong.
            >
            Ondra Holub wrote:
            srinivas napsal:
            hi Ondra
            thanks for reply.
            >
            is assignment of an iterator to another iterator valid?
            i have found this assignment is creating problem.
            >
            m_iter_item = iter_item;
            m_iter_item++;
            In general, it is valid. I think you have incorrect operator=. I do not
            know internal structures of your iterator, so I am only guessing.
            >
            srinivas
            >
            Ondra Holub wrote:
            srinivas napsal:
            hi,
            i am incrementing a list iterator "m_item" in my .cpp file
            >
            m_item++
            >
            m_item._ptr->_next is showing the valid item.
            >
            but in the library overloaded function for ++
            >
            1 >_Myt_iter operator++(int)
            2 > { // postincrement
            3 > _Myt_iter _Tmp = *this;
            4 > ++*this;
            5 > return (_Tmp);
            6 > }
            >
            (line 4)++*this is crashing...it is not showing any item in its next
            pointer.
            >
            what is the problem?
            >
            thanks,
            srinivas

            You should post here minimized compilable example which crashes.
            Without it we can only guess.
            Maybe you have to define your own implementation of assignment
            operator. If you are allocating some memory with new and freeing it in
            destructor, you definitely need define your own operator= and perform
            deep copy.

            I cannot say more, because I do not know anything about your
            implementation :-(

            Comment

            • Daniel T.

              #7
              Re: "++*this&q uot; crashing

              "srinivas" <skarlapudi@gma il.comwrote:
              i am incrementing a list iterator "m_item" in my .cpp file
              >
              m_item++
              >
              m_item._ptr->_next is showing the valid item.
              >
              but in the library overloaded function for ++
              >
              1 >_Myt_iter operator++(int)
              2 > { // postincrement
              3 > _Myt_iter _Tmp = *this;
              4 > ++*this;
              5 > return (_Tmp);
              6 > }
              >
              (line 4)++*this is crashing...it is not showing any item in its next
              pointer.
              >
              what is the problem?
              Line 4 should be: ++(*this);

              Comment

              • Daniel T.

                #8
                Re: &quot;++*this&q uot; crashing

                In article <1168856638.949 749.73610@38g20 00cwa.googlegro ups.com>,
                "srinivas" <skarlapudi@gma il.comwrote:
                hi,
                i am incrementing a list iterator "m_item" in my .cpp file
                >
                m_item++
                >
                m_item._ptr->_next is showing the valid item.
                >
                but in the library overloaded function for ++
                >
                1 >_Myt_iter operator++(int)
                2 > { // postincrement
                3 > _Myt_iter _Tmp = *this;
                4 > ++*this;
                5 > return (_Tmp);
                6 > }
                >
                (line 4)++*this is crashing...it is not showing any item in its next
                pointer.
                >
                what is the problem?
                BTW, variables starting with underscore and a capital letter are
                reserved. Don't use them unless you are writing a compiler.

                Comment

                • Daniel T.

                  #9
                  Re: &quot;++*this&q uot; crashing

                  "srinivas" <skarlapudi@gma il.comwrote:
                  >
                  is assignment of an iterator to another iterator valid?
                  i have found this assignment is creating problem.
                  >
                  m_iter_item = iter_item;
                  m_iter_item++;
                  Assignment from one object to another of the same type is valid if the
                  class was written such that it is valid. The above code does not compile
                  on its own (which does create a problem. :-)

                  You probably need to provide a valid op=.

                  Comment

                  • Ondra Holub

                    #10
                    Re: &quot;++*this&q uot; crashing


                    Daniel T. napsal:
                    "srinivas" <skarlapudi@gma il.comwrote:
                    >
                    i am incrementing a list iterator "m_item" in my .cpp file
                    m_item++
                    m_item._ptr->_next is showing the valid item.

                    but in the library overloaded function for ++

                    1 >_Myt_iter operator++(int)
                    2 > { // postincrement
                    3 > _Myt_iter _Tmp = *this;
                    4 > ++*this;
                    5 > return (_Tmp);
                    6 > }

                    (line 4)++*this is crashing...it is not showing any item in its next
                    pointer.

                    what is the problem?
                    >
                    Line 4 should be: ++(*this);
                    ++(*this) is the same as ++*this;

                    ++ (preincrement) has the same priority as * (dereference).

                    Comment

                    • srinivas

                      #11
                      Re: &quot;++*this&q uot; crashing

                      i have gone through both the list iterator
                      m_iter_item & Iter_item from begin to end.
                      using _ptr->_Next and _ptr->_Prev
                      addresses in both the iterators are identical.

                      i have no clue why ++*this is crashing.

                      code in the above case is:
                      -----------------------------------------
                      1class dummy
                      2{
                      3..
                      4..
                      5private:
                      6list<mItem>::i terator m_iter_item;
                      7}
                      8>
                      9void dummy::func1()
                      10{
                      11 list<mItem>::it erator iter_item;
                      12 getItem(&iter_i tem);
                      13 m_iter_item = iter_item;
                      14 ..
                      15 m_iter_item++;
                      16 ...
                      17}
                      -----------------------------------------
                      when i changed the function func1() as

                      9void dummy::func1()
                      10{
                      11 list<mItem>::it erator iter_item;
                      12 getItem(&m_iter _item);
                      13 ..
                      14 m_iter_item++;
                      15 ...
                      16}

                      there is no problem at all.

                      regards,
                      srinivas



                      Daniel T. wrote:
                      "srinivas" <skarlapudi@gma il.comwrote:

                      is assignment of an iterator to another iterator valid?
                      i have found this assignment is creating problem.

                      m_iter_item = iter_item;
                      m_iter_item++;
                      >
                      Assignment from one object to another of the same type is valid if the
                      class was written such that it is valid. The above code does not compile
                      on its own (which does create a problem. :-)
                      >
                      You probably need to provide a valid op=.

                      Comment

                      • srinivas

                        #12
                        Re: &quot;++*this&q uot; crashing

                        dear daniel,
                        "++*this" is in the C++ STL library, i did not write it.

                        srinivas

                        Daniel T. wrote:
                        "srinivas" <skarlapudi@gma il.comwrote:
                        >
                        i am incrementing a list iterator "m_item" in my .cpp file
                        m_item++
                        m_item._ptr->_next is showing the valid item.

                        but in the library overloaded function for ++

                        1 >_Myt_iter operator++(int)
                        2 > { // postincrement
                        3 > _Myt_iter _Tmp = *this;
                        4 > ++*this;
                        5 > return (_Tmp);
                        6 > }

                        (line 4)++*this is crashing...it is not showing any item in its next
                        pointer.

                        what is the problem?
                        >
                        Line 4 should be: ++(*this);

                        Comment

                        • Clark S. Cox III

                          #13
                          Re: &quot;++*this&q uot; crashing

                          Daniel T. wrote:
                          "srinivas" <skarlapudi@gma il.comwrote:
                          >
                          >i am incrementing a list iterator "m_item" in my .cpp file
                          >>
                          >>m_item++
                          >m_item._ptr->_next is showing the valid item.
                          >>
                          >but in the library overloaded function for ++
                          >>
                          >1 >_Myt_iter operator++(int)
                          >2 > { // postincrement
                          >3 > _Myt_iter _Tmp = *this;
                          >4 > ++*this;
                          >5 > return (_Tmp);
                          >6 > }
                          >>
                          >(line 4)++*this is crashing...it is not showing any item in its next
                          >pointer.
                          >>
                          >what is the problem?
                          >
                          Line 4 should be: ++(*this);
                          Why?

                          (hint: there is no difference between ++(*this) and ++*this).

                          --
                          Clark S. Cox III
                          clarkcox3@gmail .com

                          Comment

                          • Ismo Salonen

                            #14
                            Re: &quot;++*this&q uot; crashing

                            srinivas wrote:
                            i have gone through both the list iterator
                            m_iter_item & Iter_item from begin to end.
                            using _ptr->_Next and _ptr->_Prev
                            addresses in both the iterators are identical.
                            >
                            i have no clue why ++*this is crashing.
                            >
                            --snip--
                            Perhaps you are removing items from the list (or adding to it ) ?
                            Is your iterator still valid ( the value in it pointed in list was
                            changed or even removed ? )

                            Or the iterator was already pointing to wrong location ( at or after
                            list.end() )

                            I suppose the iterator is normal forward iterator, not reverse_iterato r.

                            Or you have memory corruption, you are overwriting the iterator fields
                            somehow.

                            ismo

                            Comment

                            • Default User

                              #15
                              Re: &quot;++*this&q uot; crashing - TPA

                              srinivas wrote:
                              hi Ondra
                              thanks for reply.
                              Please don't top-post. Your replies belong following or interspersed
                              with properly trimmed quotes. See the majority of other posts in the
                              newsgroup, or the group FAQ list:
                              <http://www.parashift.c om/c++-faq-lite/how-to-post.html>

                              Comment

                              Working...