Memories

In an effort to show you how to think like a coder, I need to introduce some basic computer science principles. To do that, I need to write a bit about how a computer works. By starting from the ground up, the plan is to build up to more complicated examples.

And though it’s kinda cliched to say, I do have a plan…

Variable and Values

In every computer language I know, there is some method for storing information for later use. You do this by creating variables and assigning values to them.

Let’s take a look at a quick code sample in Python. The following code creates a new variable called a, and assigns it the value of 5:

a = 5

That seems fairly straight forward, right? In fact, if you know JavaScript, or C, or R, or Go, or any other language, you should be able to translate this code into that language. When you do, that line of code will do the same thing in that language, namely create a variable called a and assign it the value of 5.

But what is this code really doing? What does “assign the value of 5 is to the variable a” mean to the computer? How does the computer know what a is in Python? Or in C?

Let’s take a closer look.

Random Access

There’s a lot going on in your computer, but for the sake of this post, we’ll focus on one physical thing found in every computer system – it’s memory.

File cabinet
Visualizing memory as a file cabinet

Every computer contains a set amount of memory called RAM which it uses to store information and run programs. You can think of computer RAM as a huge file cabinet filled with drawers.

Every drawer in the cabinet is identified or labeled with a unique number, which is called its address. Just like a street address can be used to find anyone’s home, you can find any drawer in RAM by using this address.

RAM addresses are always numbers, starting with the number zero (0), and continuing to the last drawer available. If your computer contains 8Gb of RAM, then the address of that very last drawer is 8,589,934,591. Using the address, we can access any portion of RAM at any time.

In fact, RAM is an acronym for Random Access Memory. It is called that because you can access any portion of it at any time. From the computer’s perspective, its memory is being accessed randomly.

Each RAM “drawer” contains one and only one piece of information, which is stored as a number. The size of that number is called a byte, and can be any number between 0 and 255. Even on a modern 64-bit computer, each memory location stores just a single byte.

Behind the Scenes

Now that you have an idea of how computer RAM works, lets take a look at what happens when you enter a = 5 in your language of choice.

Here’s the (extremely) basic (possibly over)simplified sequence:

  1. Your program asks the computer for a place to store a number.
  2. The computer gives your program the address of a memory location it can use.
  3. Your program first stores that address in a table, and labels the address as a.
  4. Your program then tells the computer to store the value of 5 in the memory address for a.

If you have a second variable, say b = 10, the same steps are followed with one major change. At step 2, the memory address returned for b will be different than the one returned for a.

This should make sense. If both a and b used the same memory address, then your program could only store one byte of data. Every variable needs its own memory location with a unique address to store its data.

Now, there’s a little (or a lot) more going on here, depending on a few factors:

  • The language you are using impacts how memory addresses are communicated to your program.
  • The exact steps may vary depending on whether you are using Windows, Linux, MacOS, or something else.
  • Code running on a website can store data in the memory of different computers.
  • There’s are other factors (which we won’t discuss now) that can complicate things even further.

In general though, these basic steps are good enough for now. We’ll refine them more as we delve into more complicated subjects.

Takeaways

There are three key takeaways and one big question you should have from this discussion:

  • Every variable you create in your program has a dedicated, unique, place in RAM to store values.

    Every time you ask for a new variable, the computer gives you a new, unique, unused memory location for it.

  • There is something in the computer, which is separate from your program, which knows what memory it can hand out.

    Keeping track of what RAM is used and unused is the job of the memory manager, which is a part of your operating system, whether it’s Windows, Mac, Linux, or something else.

  • There is a lot more happening when you run your program than just your program.

    You didn’t write any code to ask the computer for a memory address, or to store it in a table labeled a or b. We’ll get into what happens behind the scenes as this blog progresses.

And the big question?

  • How does this work with big numbers, or information which isn’t numeric like a name?

    Remember, each memory location stores a single small number. So how does the computer stores something that won’t fit?

We’ll answer that last question in the next installment…