Concepts / Storing Data in Memory

Storing Data in Memory

A variable is a named memory location that can hold data.

  • Programming

A Name for a Place That Holds Data

Every useful program works with data: a player's score, a person's age, a line of text the user typed. Before the program can use any of that data, the data has to be kept somewhere in memory while the program runs. The question this article answers is how a program keeps track of that stored data so that the programmer can work with it comfortably. The answer is the variable, and the central idea is that a variable gives a memory location a name.

As you read, keep one question in mind: when you write a name in your code, what exactly is being looked up? By the end of this article you should be able to trace the path from that name to the data it stands for.

What a Variable Is

Definition

A variable is a named memory location that can hold data.

Each part of that definition does real work. Memory location is the physical place where data is stored. Named means that location has been given a label the programmer chooses. Can hold data means the location is where a value lives while the program uses it. Put together, a variable is a labeled spot in memory that holds a value.

Programs need a way to refer to the data they use. A variable provides that way. Because the location has a name, the program can refer to the data through the name rather than needing to know where the location itself is.

Tracing a Name to Its Data

A variable involves three things that are easy to blur together: the name you write, the memory location that name is associated with, and the data stored in that location. The source describes the relationship directly: the name identifies the location, while the location holds the data. Follow the chain from left to right in the diagram below.

scorename written in the codememory locationthe named location10data held thereidentifiesholds
When I write a variable name, how do the name, the memory location it refers to, and the value stored there line up?

Read the diagram as a chain. The name score identifies a memory location. That location holds the value 10. The value does not sit in the name, and the name is not the location. The name is the handle the programmer uses to reach the location, and the location is what actually holds the data.

Why Names Replace Locations

The programmer refers to a variable by its name, not by its memory location. This choice keeps attention on meaningful names such as score rather than on the physical location of the storage. A name like score tells you what the data is for. A location tells you only where it sits, which is a detail that has nothing to do with the problem the program is solving.

Think of the difference between saying 'the value in score' and 'the value in the storage location that was assigned to score.' Both describe the same data, but the first is what a programmer needs to reason about. Naming the data keeps the code readable: when you see score in a program, you know the purpose of the data without decoding where it lives.

Where the Compiler Fits

If the programmer writes names instead of locations, something has to connect each name to a real memory location. The source assigns that job to the compiler: the compiler is responsible for keeping track of the memory location associated with each name. The programmer does not have to manage that bookkeeping by hand.

This division of labor is the reason names are useful. The programmer writes score and reasons about it as a meaningful quantity. The compiler handles the link from score to the location where the data is kept. The two responsibilities stay separate, and each side can focus on its own job.

Watching a Value Change

Now consider what happens when the data for a name changes. The name stays fixed, and the name still identifies the same memory location. What changes is the data held in that location. The diagram below shows the same name before and after its value is updated.

scorename10data in the locationscorename25data in the locationholdsholds
When the value for score changes, does the name change, or only the data held in its location?
Worked example

Tracing a score through an update

A program stores 10 under the name score, and later stores 25 under the same name. Which parts stay the same and which part changes?

  1. 1
    Start with the name

    The programmer writes score. The name identifies one memory location, and that association is what the compiler keeps track of.

  2. 2
    Store the first value

    The location associated with score holds 10. The programmer reasons about this as the score, not as an address.

  3. 3
    Store a new value under the same name

    The program places 25 in the location that score refers to. The name is unchanged, and the location is still the same named location.

  4. 4
    Read the name again

    Any later use of score now produces 25, because the name leads to the location and the location now holds 25.

Result

The name score and its association with the memory location stay the same. Only the data held in that location changes, from 10 to 25.

Mistakes Beginners Make with Variables

  • Common mistake

    Treating the variable name as if it were the stored data

    What it looks like

    Thinking that score is the number 10 by itself, rather than a name for the location that holds 10.

    Why it fails

    The source says the name identifies the location, while the location holds the data. The name and the data are two different things.

    Do this instead

    Read score as 'the named location called score', and then ask what value that location currently holds.

  • Common mistake

    Assuming the programmer must know the physical location

    What it looks like

    Trying to write code that depends on where score is stored in memory.

    Why it fails

    The programmer refers to a variable by its name, not by its memory location. The compiler tracks the location for the name.

    Do this instead

    Use the name in your code and let the compiler handle the link between that name and its memory location.

  • Common mistake

    Believing that changing the value changes the name

    What it looks like

    Expecting that after storing 25, the variable is now called something else.

    Why it fails

    The name stays associated with the same location. Only the data held there changes.

    Do this instead

    Keep the name stable and think of updates as changes to the contents of the named location.

Practice Tracing a Name

EASY

A variable named age is a memory location that holds the data 21. Without using any actual memory addresses, explain in your own words the three parts involved: the name, the memory location, and the data. Then say which part the compiler tracks and which part the programmer chooses.

Hints
  • The source says the name identifies the location, and the location holds the data.
  • The programmer chooses the name. The compiler keeps track of the memory location associated with that name.

Key Takeaways

  • A variable is a named memory location that can hold data.
  • The name identifies the location, and the location holds the data.
  • Programmers refer to variables by meaningful names such as score rather than by their memory locations.
  • The compiler is responsible for keeping track of the memory location associated with each name.
  • Updating a variable changes the data held in its location, while the name stays the same.