Chapter 2. Variables and Memory

To write your C++ game program, you will need your computer to remember a lot of things. Things such as where in the world is the player, how many hit points he has, how much ammunition he has left, where the items are in the world, what power-ups they provide, and the letters that make up the player's screen name.

The computer that you have actually has a sort of electronic sketchpad inside it called memory, or RAM. Physically, computer memory is made out of silicon and it looks something similar to what is shown in the following screenshot:

Variables and Memory

Does this RAM look like a parking garage? Because that's the metaphor we're going to use

RAM is short for Random Access Memory. It is called random access because you can access any part of it at any time. If you still have some CDs lying around, they are an example of non-random access. CDs are meant to be read and played back in order. I still remember jumping tracks on Michael Jackson's Dangerous album way back when switching tracks on a CD took a lot of time! Hopping around and accessing different cells of RAM, however, doesn't take much time at all. RAM is a type of fast memory access known as flash memory.

RAM is called volatile flash memory because when the computer is shut down, RAM's contents are cleared and the old contents of RAM are lost unless they were saved to the hard disk first.

For permanent storage, you have to save your data into a hard disk. There are two main types of hard disks, platter-based Hard Disk Drives (HDDs) and Solid-state Drives (SSDs). SSDs are more modern than platter-based HDDs, since they use RAM's fast-access (Flash) memory principle. Unlike RAM, however, the data on an SSD persists after the computer is shut down. If you can get an SSD, I'd highly recommend that you use it! Platter-based drives are outdated. We need a way to reserve a space on the RAM and read and write from it. Fortunately, C++ makes this easy.