Thursday, March 24, 2011

Pointers and Walk-In Closets

Pointers are variables that hold a particular type of data: addresses in the computer's memory. A main advantage of using pointers is the flexibility that they provide when interacting with data stored in memory. A single, relatively small, pointer can give the location of a massive chunk of data sitting in memory, allowing functions to access the data without having to pass around the full block of data itself. Programmers can also create complex data structures by using pointers to link different blocks of data.

The powerful Wizard Marcus lived in a small apartment in downtown New Athens. The rent was high and the views were terrible, but that was the price of living downtown. To be honest, neither of those factors bothered Marcus. What annoyed him was the size of the apartment. Marcus had accumulated many belongings over the years, and he liked having a place to put them all. The apartment had only one pathetically small closet.

Being a wise and powerful magician, Marcus turned to magical solutions to make his life better. First, he shrunk all of his belongings so that they would all fit in his closet. This created quite a mess. There were so many tiny items thrown into the closet that he could never find anything. One night it took him hours of searching with a magnifying glass in order to find his favorite wizard's hat. As a result, he was tremendously late for a date.

Next, he tried dehydrating all of his possessions down to powder and putting them in labeled jars. His closet soon resembled a spice rack filled with hundreds of tiny jars. Unfortunately, rehydrating cloths left them thoroughly soaked, and a single poorly timed sneeze scattered his favorite cloak over his living room. Marcus quickly abandoned that idea as well.

Finally, he realized that he did not need to actually keep all of his possessions “right here” as long as he could easily retrieve them. He bought a huge castle in the rural outlands to store his belongings. When he needed an object, he would summon it. Similarly, when he was done with using it, he would send it back. The process effectively gave him a giant virtual closet.

In only a short matter of time, Marcus found that he had a new problem. He no longer remembered what was in the castle or even how much space he had left. He found himself randomly summoning items from different rooms in the castle just to see what was there. Marcus needed a better system.

In order to keep track of what he stored in the castle and where it was, Marcus created a formal accounting system. He divided the castle into a giant labeled grid of squares, each indicating different spaces where items could be. Then he created a scroll listing all of his items. Each time he sent an item to the castle, he carefully recorded a pointer to where that object was sent. Using this scroll he could quickly find the location from which to summon any item. He only needed to keep one small scroll of pointers in his downtown apartment. This system worked great.

Or, at least, the system worked great most of the time. Occasionally, Marcus got careless and forgot to record where he sent an item. As a result the item was lost and the space inside the castle wasted. Other times, Marcus would forget to check that a space was free before sending an object there. One night, after returning from a party, he accidentally sent his coat to the same location as a priceless vase. The vase exploded as the large coat appeared inside it. These were the inherent dangers of carelessness with his pointers.

Despite the occasional accident, the system worked well. Marcus learned to be careful about tracking everything in his scroll, and he was able to live for many years in the most fashionable neighborhoods.

2 comments:

  1. One day, he accidentally tried to summon stuff from "room 0" and the castle burned to the ground.

    Then he'd remembered he'd put an auto-destruct button in room 0 to remind himself not to do this. In retrospect, the design was a bit draconian.

    TK

    ReplyDelete

Note: Only a member of this blog may post a comment.