Endless Looping with const_iterator

This is a discussion on Endless Looping with const_iterator within the C++ Programming forums, part of the General Programming Boards category; Hi All, I am encountering issue whereby my function enters an infinite loop in the below example. Code: Function a{ ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    Singapore
    Posts
    3

    Endless Looping with const_iterator

    Hi All,

    I am encountering issue whereby my function enters an infinite loop in the below example.

    Code:
    Function a{
          :
    const sparse_data& sparse = ra_data->sparse(x, y, site, colum); 
    for(sparse_data::const_iterator si = sparse.begin(); si != sparse.end();) {
    
        // Invoked some function B that alters the content of sdata->sparse
        // Does si++ in the for loop
    
    }
         :
    }
    I tried removing the referencing and this actually solved my problem.

    Code:
    Function a {
            :
    const sparse_data sparse = sdata->sparse(x, y, site, column); 
    for(sparse_data::const_iterator si = sparse.begin(); si != sparse.end();) {
    
        // Invoked some function B that alters the content of sdata->sparse
        // Does si++ in the for loop
    
    }
              :
    }
    I was hoping someone can guide me on why does this happen.

    My guess on the issue is that the function B inside the for loop changes the sparse since it is a reference to sdata->sparse. Hence when the for loop checks the end condition, si = sparse.end is no longer valid fulfiled.

    Apologies for the lack of actual coding as they are proprietary information of my company.
    Last edited by CH_Tang; 05-19-2008 at 07:56 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,302
    The problem looks simpler than you think.
    There's no ++si in the loop.
    I'd say that the copy-constructor for sparse_data doesn't copy sparse, hence in the second one the container is empty and it never enters the infinite loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    Singapore
    Posts
    3
    My bad. Forgot to put the si++ in my pesudo code. Thanks for the prompt reply

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    So, is the problem solved?

    If not, it is pretty much impossible to tell without some actual code. One strange thing is that you have const all over the place, yet the purpose of the loop is to alter things...
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    May 2008
    Location
    Singapore
    Posts
    3
    Yes I managed to find my solution.

    You are correct as the alteration changed my for loop end condition. In the end it became unachieveable.

    Thanks for all your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 12:43 PM
  2. Exiting Endless Loop
    By Phoenix_Rebirth in forum C Programming
    Replies: 4
    Last Post: 11-23-2008, 05:02 PM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 01:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2003, 11:01 PM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 06:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21