Iterative Loops: Iterators

    Iterative Loops: Iterators

      It's rude to point….unless you're a computer, of course. Sure, in real life people get a little peeved when you shove a finger in their face (even if it's to compliment their nice smile). Without pointing in computers, though, not a lot would get done.

      Computers live to point. These pointers don't involve fingers, toes, or lasers, though. No, pointers in computer science are simply variables that store addresses in computer memory—usually written in hexadecimal (base-16). Think of these addresses like your home address: they're where the data lives in computer memory. In the case of for loops, pointers make sure the loop runs correctly.

      Where exactly, in memory, does a list begin? Its pointer will tell you. If your loop runs over a list (which happens, uh…a lot), your for loop will start off with a pointer to the first element of that list.

      Loops and Lists

      Most lists are stored sequentially in memory as arrays, with elements falling into line one right after the other. Even if a list isn't stored exactly this way (maybe it's a linked list or another data structure that just looks like a list to the programmer), each element usually has a pointer in it with the address of the next element of the list, linking them all together like a daisy chain.

      Every time the loop iterates, that pointer's going to adjust to hit the next element in the list. That way your computer will never get lost in the list while iterating. It'll always know exactly what element it's looking at. For this reason, before your program actually starts to loop, it needs to know (or calculate) how big each element of the list is so that it knows how far it needs to travel in address space to hit the next element. If the elements are huge, this step size will be just as large; smaller elements lead to a small step size.

      If everything works right, the iterator will hit the beginning of next element. If not…there's going to be a memory issue with hitting data at the wrong place. Your loop would break down almost instantly on account of it not accessing the data it was supposed to access. For example, in a list with elements that are of size 4 bytes, you don't want the pointer to access your data in 1, 2, or 3 byte chunks. If it did, you'd only be grabbing part of that element's information and your program would get seriously messed up.

      If, on the other hand, the chunks of data were too big, say 6 or 8 bytes, you'd be getting the entirety of one element…along with some of the next element. That's going to give a bad output on so many levels.

      Here's where iterators come in.

      Letting the Iterator do It for You

      To help computers with all of the pointer adjustments and size calculations, most programming languages have some sort of iterator built into them. Behind every for loop, there's an iterator hard at work making calculations to keep track of where everything is in the data structure the loop's running through.

      Iterators can easily reference that beginning list pointer along with the list element size. That way, iterators know exactly what element in the list the loop's currently working on and help ensure it doesn't try to access memory beyond where the list is stored. That would most likely lead to the dreaded SegFault—where the computer catches the code trying to access data where it shouldn't be. Big oops.

      Whenever a list's working properly, remember there's an iterator behind the scenes to thank for it. You might want to send it chocolate every once in a while to show your thanks.

      No one likes being pointed at, but everyone likes being noticed for their work.