"++*this" crashing

Discussion in 'C++' started by srinivas, Jan 15, 2007.

  1. srinivas

    srinivas Guest

    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
    srinivas, Jan 15, 2007
    #1
    1. Advertising

  2. srinivas

    Ondra Holub Guest

    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.
    Ondra Holub, Jan 15, 2007
    #2
    1. Advertising

  3. srinivas

    srinivas Guest

    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.
    srinivas, Jan 15, 2007
    #3
  4. srinivas

    Ondra Holub Guest

    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.
    Ondra Holub, Jan 15, 2007
    #4
  5. srinivas

    srinivas Guest

    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.
    srinivas, Jan 15, 2007
    #5
  6. srinivas

    Ondra Holub Guest

    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 :-(
    Ondra Holub, Jan 15, 2007
    #6
  7. srinivas

    Daniel T. Guest

    "srinivas" <> wrote:

    > 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);
    Daniel T., Jan 15, 2007
    #7
  8. srinivas

    Daniel T. Guest

    In article <>,
    "srinivas" <> wrote:

    > 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.
    Daniel T., Jan 15, 2007
    #8
  9. srinivas

    Daniel T. Guest

    "srinivas" <> wrote:
    >
    > 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=.
    Daniel T., Jan 15, 2007
    #9
  10. srinivas

    Ondra Holub Guest

    Daniel T. napsal:
    > "srinivas" <> wrote:
    >
    > > 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).
    Ondra Holub, Jan 15, 2007
    #10
  11. srinivas

    srinivas Guest

    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:
    -----------------------------------------
    1> class dummy
    2> {
    3> ..
    4> ..
    5> private:
    6> list<mItem>::iterator m_iter_item;
    7> }
    8>
    9> void dummy::func1()
    10> {
    11> list<mItem>::iterator iter_item;
    12> getItem(&iter_item);
    13> m_iter_item = iter_item;
    14> ..
    15> m_iter_item++;
    16> ...
    17> }
    -----------------------------------------
    when i changed the function func1() as

    9> void dummy::func1()
    10> {
    11> list<mItem>::iterator 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" <> wrote:
    > >
    > > 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=.
    srinivas, Jan 15, 2007
    #11
  12. srinivas

    srinivas Guest

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

    srinivas

    Daniel T. wrote:
    > "srinivas" <> wrote:
    >
    > > 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);
    srinivas, Jan 15, 2007
    #12
  13. Daniel T. wrote:
    > "srinivas" <> wrote:
    >
    >> 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
    Clark S. Cox III, Jan 15, 2007
    #13
  14. srinivas

    Ismo Salonen Guest

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

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

    ismo
    Ismo Salonen, Jan 15, 2007
    #14
  15. srinivas

    Default User Guest

    Re: "++*this" 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.com/c++-faq-lite/how-to-post.html>
    Default User, Jan 15, 2007
    #15
    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. Nicolas LeBlanc

    SmartNavigation & application crashing..

    Nicolas LeBlanc, Jul 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    417
    Ravikanth[MVP]
    Jul 16, 2003
  2. Temp

    Browser Crashing... ????

    Temp, Aug 9, 2003, in forum: ASP .Net
    Replies:
    2
    Views:
    315
    MS News \(MS ILM\)
    Aug 9, 2003
  3. Cheung, Jeffrey Jing-Yen

    Help!!! ASP.NET Crashing in production environment

    Cheung, Jeffrey Jing-Yen, Aug 13, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    371
    Cheung, Jeffrey Jing-Yen
    Aug 13, 2003
  4. Kathy Burke
    Replies:
    2
    Views:
    347
  5. Steve Roszko
    Replies:
    0
    Views:
    331
    Steve Roszko
    Dec 4, 2003
Loading...

Share This Page