<<== <= [table of contents] [search] [glossary] [feedback] =>

[online C++ tutorial]Section 8: Pointers and Memory Management

Section 8.1: What is a Pointer?

So what is a pointer? A pointer is a way to get at another object. Essentially it is a way to grab an instance of an object and then either pass that instance a message or retreive some data from that object. A pointer is actually just an address of where an instance is held in memory.

Some piece of your program can either possess an instance of an object, or know where an instance of an object is. An instance of an object is a chunk of memory that is big enough to store all the member data of that object. A pointer is an address that explains how to get to where the instance is actually held in memory. Here's a quick example:

Our Player object has three pieces of data that it owns: strength, agility, and health. These are a part of the player object. That makes sense in real world terms. The player knows about two other pieces of data: the weapon and the armor that the player possesses. Here's a diagram for an instance of the player object.

Figure 1

So that is how to conceptually think of pointers. Now what's really going on? Memory in a computer is a complicated thing, but let's reduce it to it's simplest form: one large string of slots with addresses that data can be put in. As in the following picture:

Figure 2
If we were to access the spot in memory with address 3, we would get the value 45. If we were to access the spot in memory with address 2 we would get the value "Dave". The previous diagram over simplifies two important concepts, however. First, each spot that has an address is the same size as every other spot. Second, what's held in memory is simply data. That is, the information there is just a string of binary data, 1's and 0's. The way that the data is viewed gives it meaning.

With these new ideas in mind let's Take a look at our previous diagram about the player and see what's really going on. Here is the pseudo code for the Player in a little more detail then we have seen:

class Player {
  // attributes
  int     health;
  int     strength;
  int     agility;

  // pointers to weapons and armor
  Pointer weapon;
  Pointer armor;

  // actions that a player knows how to perform
  void    move();
  void    attackMonster();
  void    getTreasure();
};

What follows could be a view of memory. This will seem fairly complex and is not imperative to understanding the c++ language. If it does not make sense right away, come back to it.

I have translated the actual binary values to their corresponding values in the context of our program. In this view an instance of a Player is stored at memory address 4098. The first attribute (instance variable) is there: health is 12. The second attribute is strength, 14. Then agility 16. The next attribute, stored at address 4104 is a pointer to an instance of a weapon object. If we follow that to the bottom of the diagram, there is the name of the weapon, "club" stored as ASCII values at address 6144 through address 6148 (the 0 ends the string). The next attribute for a weapon after name is rating, in this case 2, which means "poor" because I have decided that the rating system should be:

1 = very poor
2 = poor
3 = average
4 = good
5 = very good

Notice that all the memory contains is binary data. It is how that data is viewed that actually gives it meaning.

Figure 3

<<== <= [table of contents] [search] [glossary] [feedback] =>