HOW TO REMEMBER STUFF
Previous lessons discussed programming logic and the organization of programs with subroutines. They also talked about markup languages and how they're used to add to extra information to data documents. This lesson covers how you can save and read data, and how that data is stored in memory so your program can work on it.
Different Types of Computer Memory
In the technological saga of the computer revolution, memory has always been one of the dominant factors. Early computers used some bizarre methods of data storage; for example, the first Univac stored data as vibrations propagating through thin columns of mercury. (Really! This isn't a fib! Look here). Imagine a room full of Fastrand memory drums rumbling like a power plant and holding less data than a single hard drive in a modern laptop.
As CPUs (central processing units) got faster and more compact, the limitation of the speed of light became significant and computer designers concentrated on keeping memory as close as possible to the CPU.
Now, computers remember data with a variety of technologies. As a programmer, you need to understand what these technologies imply. Table 6-1 lays out the territory for further exploration.
| Memory | Speed | Volatility | Capacity | Programming Techniques |
| CPU cache | Ultra fast | Volatile | Limited -- hundreds of kilobytes (thousands of bytes | The programmer has no way to influence |
| Main chip memory | Fast | Volatile | Many megabytes (millions of bytes) | Allocating space for variables, especially arrays and objects |
| Flash memory | Fast | Nonvolatile | Kilo to megabyte | Specialized according to hardware |
| Hard disk memory | Fairly fast | Nonvolatile | Many gigabytes (billions of bytes) | Creating, reading, and writing files on file systems with serial and random access methods |
| Removable media, floppy disk, CD, DVD, tape | Varies | Nonvolatile | From megabyte to many gigabytes | File systems -- creating, reading, and writing files with serial and random access |
| Network database | Medium | Nonvolatile | Gigabytes to terabytes | Query systems such as SQL |
Table 6-1: Characteristics of various kinds of memory.
Modern CPUs have a chunk of high-speed memory, called cache memory, directly on the CPU chip. Data is moved to and from the main chip memory by CPU logic that tries to predict what the program is going to want next. This improves the overall speed by having needed data in the fastest possible memory. There's no good way for the programmer to influence this process.
Main chip memory uses an enormous number of tiny transistors to hold the bytes that your program uses for program code and data. Even the slightest loss of power can cause these transistors to forget everything. The language you use has a lot to do with how you manage this memory, as you learn later in this lesson.
Flash memory is a special kind of transistor-based memory that can hold a value after the power goes off. This is the memory that holds your favorite TV channel settings when the power goes off, or holds your digital camera pictures in a memory stick. Usually the programmer has to do something special to use flash memory unless the operating system makes it look similar to a file system.
Hard disk memory stores data as magnetic patterns that can be read or written under the control of your operating system. It's the operating system's responsibility to present this data to your program in the form of a file system.
There are several varieties of removable media in use these days, from floppy disks to DVDs. Your operating system generally handles the hardware differences and presents the programmer with a consistent file system interface. Flash memory, hard disk storage, and removable media are collectively called nonvolatile storage because continuous power is not required to hold the data.
When storing data in a database -- particularly if the database management is handled by someone other than you -- the programmer's life is greatly simplified. Communication with databases is discussed in Lesson 7.
Bits, Bytes, and Word Size
Historically speaking, computers designers have tried many different ways to organize memory. Fortunately for computer users, designs have converged on using an 8-bit byte as the basic unit of memory. One reason for this convergence is that with 8 bits, you can represent all of the numerals, punctuation, and characters in English text, with some left over for special characters.
Although the original microcomputers read and wrote to memory one byte at a time, modern CPUs read and write in 32- or 64-bit (4 or 8 byte) chunks. These details are hidden from the programmer, however, and the fundamental unit that you work with is still the byte.
