A girl and a robot thinking together

Think together · Start with a story

A girl draws a box labeled score. The robot asks: are the box's name and address the same thing?

Break down the process

Follow the numbered steps, then explain them in your own words
  1. Name in codescore
  2. Storage locationWhere data is kept
  3. Current contentThe value 8

Programs use variable names to refer to data. In a memory model, an address can be thought of as a number that identifies a storage location.

A name makes code easier to write, an address describes a location, and a value is the content stored there.

01Why are variables connected to memory when a program runs?

A running program needs places to keep values it is using. Memory is one way to provide storage for those values.

int score = 8;

The variable name score helps the programmer refer to the value. The program and compiler handle how that value is represented and used.

02Is memory made of many little boxes?

It can help to imagine memory as a row of numbered locations. Each addressable byte has an address.

Address 10001001100210031004

Real memory management is much more complex, but it is correct that each byte location can be identified by an address.

03How many bytes does a variable use?

It depends on the variable's type and the platform.

int age = 10;
char grade = 'A';
bool ok = true;

These types do not necessarily use the same number of bytes. Use sizeof to check how many bytes a type or object occupies.

std::cout << sizeof(int);

04Does a variable have its own address?

Many objects that exist in memory have an address. In C++, use the address-of operator & to get an object's address:

int score = 95;
std::cout << &score;

An address is like a location number that helps identify an object.

05Does a variable always really use RAM?

Not necessarily. This is an important correction.

A compiler may keep some values in CPU registers, or optimize a calculation so that a variable does not need a separate memory location at all.

06Are all local variables on the “stack”?

In many implementations, function-local variables often use stack space. But this is not an absolute requirement of the C++ language standard.

If a compiler puts a variable in a register or removes it through optimization, it may not actually use a location on the stack.

What you have learned

  • When a program runs, data for many variables is kept in memory.
  • Addresses can identify bytes in memory.
  • The type and platform determine how much space a variable uses.
  • sizeof can check the size of a type or object.
  • A variable does not always correspond to one fixed cell in RAM; a compiler may use a register or optimize it away.
  • “Local variables are always on the stack” is not a strict language rule.

Next: int: How Are Integers Stored?

Your turn to try

If you change score from 8 to 9, does the variable's name have to change too?

Ready? Open to see the explanation

No. The name can still be score; only the current value changes. It is like erasing the old score on the same scorecard and writing a new one.