Variables, Memory, and const
What you'll learn
- Separate a name in source code from the memory it refers to at runtime
- Understand type, address, size, and alignment without getting bogged down in jargon
- Distinguish objects and entities, and see why functions and references are not ordinary data objects
- Tell top-level
constapart from low-levelconstacrossint,int*,const int*, andint* const - Avoid the most common beginner traps around
constand pointers
Motivation
Suppose you just learned how to write int x = 10; and int* p = &x;. Everything makes sense until you add const and hit a puzzling contrast:
const int x = 1;
x = 2; // Compiler error: cannot assign to variable 'x' with const-qualified type
int y = 20;
const int* p = &x;
*p = 5; // Compiler error: read-only variable is not assignable
p = &y; // Compiles! Why is p allowed to point somewhere else?At first glance, that looks contradictory. Why can x not be reassigned, why can *p not be modified, and yet p = &y; is completely fine?
To answer those questions cleanly, you need a mental model of how C++ connects names, types, memory, and const. When those pieces are blurred together, const feels arbitrary. When they are separated, the rules become consistent and predictable.
When this matters most
Reading function signatures (const std::string&, const char*), understanding compiler errors involving const, writing safe pointer/reference code, and preparing for references, move semantics, and constexpr.
When you can ignore it
Only if you never read or write const, pointers, or references — which is almost impossible in real C++, since standard libraries and APIs use them everywhere.
First, separate the name from the box
A variable name in your code (like x or p) is not the memory itself. The name is a label you write in source code; the memory is storage at runtime. Confusing the label with what sits inside or outside the box is where const confusion begins.
Core Idea & Intuition
First, names, types, and memory
When you write a variable declaration such as:
int x = 10;three distinct concepts come into play:
- Name (identifier): The spelling
xin your source code. It lets you refer to a thing in your program. A name is a compile-time convenience for humans; the CPU works with addresses and machine instructions. - Type: The classification
int. A type tells the compiler how to interpret the raw bits in memory (for example, as a two's-complement integer rather than floating-point numbers or characters) and which operations are allowed (+,-, assignment, etc.). - Memory space: Storage set aside for the value at runtime. That memory has three key properties:
- Address: Where the storage begins in the program's address space (often printed in hexadecimal).
- Size: How many bytes that storage occupies (for example,
sizeof(int)is commonly4bytes on desktop platforms). - Alignment: A rule stating that the object's address must be a multiple of a certain byte boundary (for example, a 4-byte
intoften prefers or requires an address divisible by4) so hardware can read and write it efficiently.
Think of a type as the blueprint/rulebook and the memory space as the physical storage where bytes live. Later on, you will also see how storage duration (when raw bytes are allocated and reclaimed) differs from object lifetime (when a valid, initialized object actually exists in those bytes) — for now, we focus on how names, types, and const govern reading and writing.
The intuition, in one sentence
A variable name is a label pointing to a typed region of memory, and const restricts how you may access or modify what that label reaches — not necessarily every bit in the universe.
Objects vs. entities (and why functions and references are special)
In everyday speech, people call almost anything in code an "object." In C++, the term has a specific meaning:
- Object: A region of storage in the execution environment that holds a value of some object type (such as an
int, adouble, an array, a struct, or a pointer). Even a pointer is an object: its value is an address, and that address itself lives in memory somewhere. - Entity: A broader umbrella term in C++ for named or identifiable program constructs — including objects, functions, references, namespaces, types, templates, and their members.
Why does this distinction matter? Because not every entity is an object:
- Functions are entities (you can name them and call them), not data objects holding values in writable variable boxes.
- References (such as
int& ref = x;) are entities — aliases bound to an existing object — rather than data objects themselves in C++'s language model (even if a compiler/ABI may allocate storage for a reference in certain contexts, such as class members or function arguments).
Analogy — and where it breaks
Locker labels and viewing windows
Imagine a row of lockers in a hallway.
A name (x) is a nametag clipped onto a locker door. The type (int) tells you what kind of item fits and how to read it. The locker's address is its number in the hallway, its size is how wide the locker is, and its alignment is the spacing rule on the wall.
Now picture const:
- Putting
conston the item inside (const int) is like putting a glass cover over the item: you can look at it, but you cannot reach in and swap it out through thatconstview. - Putting
conston the pointer itself (int* const) is like gluing the nametag/strap so the pointer cannot be re-aimed at another locker — even if you can still open the locker it points to.
Where the analogy breaks
In the real world, a physical lock on a locker can stop anyone from touching the item. In C++, const is primarily a compile-time access rule on a type/expression, not an unbreakable hardware lock on physical RAM.
If you have a non-const variable int a = 10; and a const int* p = &a;, the underlying memory of a is still mutable through a. Furthermore, if someone illegally casts away const with const_cast on an object that was originally defined as const, modifying it causes undefined behavior — C++ does not guarantee a neat runtime error; anything can happen.
Top-level const vs. low-level const
When pointers enter the picture, there are two places where const can appear:
- Top-level
const: Applies to the object itself (for example, the pointer object itself cannot be reassigned to point elsewhere, or a non-pointer variableconst int x = 1;cannot be modified). - Low-level
const: Applies to the pointed-to (or referenced) type — meaning you cannot modify the value through that pointer/reference.
A classic reading rule helps: read a type declaration from right to left, starting at the identifier:
int x—xis anint.const int x(same asint const x) —xis aconst int(top-levelconstonx).const int* p—pis a pointer (*) to aconst int. Theconstis low-level (the pointed-tointisconst;pitself can point to anotherint).int* const p—pis aconstpointer (constafter*) to anint. Theconstis top-level (the pointerpitself cannot be changed to point elsewhere; theintit points to can be changed via*p).const int* const p—pis aconstpointer to aconst int. Both low-levelconst(cannot modify*p) and top-levelconst(cannot reassignp) apply.
Formal Definition / Mechanism
How C++ models storage, types, and const
- Storage duration & layout: An object occupies contiguous bytes of storage with a determined size (
sizeof(T)) and alignment requirement (alignof(T)). You can obtain the address of most objects with the address-of operator&, whereas non-addressable constructs and expressions — such as bit-fields (which are objects in C++, but do not have an address) and temporary/rvalue expressions — cannot have their address taken with&. - Type qualifiers:
constis a type qualifier. Addingconstto a type produces aconst-qualified version of that type, restricting operations that mutate values through that qualified type. - Pointer declarator rules: In a pointer declarator,
constbefore*modifies the pointee type (low-level relative to the pointer), whereasconstafter*modifies the pointer itself (top-level on that pointer). - Implicit conversion to
const: In C++, you can implicitly convert a non-constpointer to a pointer toconst(for example,int*toconst int*), addingconstprotection. The reverse — implicitly convertingconst int*toint*— is forbidden because it would silently dropconstprotection.
Two subtleties worth memorizing
const int and int const mean the exact same thing. Placing const before or after the base type specifier (int) produces the identical type. Consistency in your codebase matters more than which order you pick, though you must read const on the right of * as applying to the pointer itself.
const does not automatically make a runtime value a compile-time constant. In C++, const int n = get_runtime_value(); is valid: n cannot be modified after initialization, yet its value is only known at runtime. (Compile-time constant expressions are a related, stricter concept, often associated with constexpr.)
Quick comparison of int and pointer const forms
| Declaration | Can modify x directly? | Can reassign p (p = &other)? | Can modify pointee via *p (*p = val)? | Which const is present? |
|---|---|---|---|---|
const int x = 1; | No | N/A (not a pointer) | N/A | Top-level on x |
int* p = &a; | N/A | Yes | Yes | None |
const int* p = &a; | N/A | Yes | No | Low-level (on pointee) |
int* const p = &a; | N/A | No | Yes | Top-level (on p) |
const int* const p = &a; | N/A | No | No | Both low-level and top-level |
How const interacts with pointer operations
Step-by-Step Example
Let's walk through a small, self-contained program that inspects sizes, addresses, and const behavior step by step.
Step 1 — inspect size, alignment, and address of a variable
#include <iostream>
int main() {
int x = 10;
std::cout << "sizeof(x): " << sizeof(x) << " bytes\n";
std::cout << "alignof(x): " << alignof(x) << " bytes\n";
std::cout << "address of x: " << static_cast<const void*>(&x) << "\n";
}sizeof(x)reports how many bytes the objectxoccupies.alignof(x)reports the alignment requirement ofx's type (int).&xobtains the address ofx. Casting toconst void*explicitly formats the value as a generic pointer address instd::cout(which is especially important when printingchar*orconst char*values so they are not treated as C-style strings).
Step 2 — see how a pointer is its own object with its own address
#include <iostream>
int main() {
int x = 10;
int y = 20;
int* p = &x;
std::cout << "x value: " << x << ", &x: " << static_cast<const void*>(&x) << "\n";
std::cout << "p value (target addr): " << static_cast<const void*>(p) << "\n";
std::cout << "&p (pointer's own addr): " << static_cast<const void*>(&p) << "\n";
p = &y; // p now holds y's address
std::cout << "after p = &y, *p: " << *p << "\n";
}xandplive in different memory locations (&xand&pare different addresses).- The value stored inside
pis the address ofx, and later the address ofy.
Step 3 — compare top-level const and low-level const on pointers
#include <iostream>
int main() {
int a = 10;
int b = 20;
// 1. Low-level const: cannot change *pLow, can change pLow
const int* pLow = &a;
std::cout << "*pLow = " << *pLow << "\n";
// *pLow = 15; // Error: pointed-to int is const via pLow
pLow = &b; // OK: pLow itself is not const
std::cout << "after pLow = &b, *pLow = " << *pLow << "\n";
// 2. Top-level const: cannot change pTop, can change *pTop
int* const pTop = &a;
*pTop = 99; // OK: a is modified through pTop
std::cout << "a is now " << a << "\n";
// pTop = &b; // Error: pTop itself is const
// 3. Both low-level and top-level const
const int* const pBoth = &a;
std::cout << "*pBoth = " << *pBoth << "\n";
// *pBoth = 50; // Error
// pBoth = &b; // Error
}Walkthrough of what happens:
pLowprotects the target (const int) from being written throughpLow, while still lettingpLowpoint tob.pToplocks the pointer (int* const) so it always points toa, while still letting you write toathrough*pTop.pBothlocks both.
Key insight
When reading a pointer declaration, look for where const sits relative to *: left of * mainly restricts what you reach by dereferencing (*p); right of * restricts the pointer variable p itself.
Step 4 — observe that const on a view does not freeze a mutable owner
#include <iostream>
int main() {
int original = 100;
const int* view = &original;
std::cout << "before: " << *view << "\n";
original = 250; // Mutated through the non-const owner 'original'
std::cout << "after modifying 'original', *view is: " << *view << "\n";
}view promised not to modify original through view. It did not promise that original could never change anywhere else in the program.
Common Misunderstandings & Pitfalls
Pitfall 1 — Treating a const view as unchangeable memory
False
If you have const int* p = &x; where x is a non-const int, x can still be modified through x (or another non-const pointer). const on p restricts you when using p; it does not make x's bytes read-only in hardware.
Pitfall 2 — Confusing const int* and int* const
False
Beginners often assume const int* means "constant pointer." In fact, const int* means "pointer to const int." To make the pointer itself const, put const after *: int* const.
Pitfall 3 — Assuming const always means "evaluated at compile time"
Subtle distinction
In C++, const means read-only through this name/access path, not necessarily compile-time constant. A const variable can be initialized from a runtime expression (such as user input or a function return value). If you specifically need a compile-time constant, look into constexpr as you advance.
Pitfall 4 — Thinking a const variable can never be initialized
Initialization is not assignment
You must initialize a const variable when you define it (because you cannot assign to it later):
const int k = 42; // Initialization: valid
// k = 43; // Assignment: errorInitializing is setting its initial value when created; assigning is overwriting it afterward.
Pitfall 5 — Trying to silently drop const from a pointer
False
int y = 10;
const int* cp = &y;
int* p = cp; // Error: cannot convert 'const int*' to 'int*'Even when y itself is non-const, C++ forbids implicitly converting const int* to int* so you cannot silently drop the const protection of cp and write *p = 20;. And if you force it (such as with const_cast) on an object that was originally defined as const (const int x = 10;), modifying it causes undefined behavior.
Pitfall 6 — Calling functions or references "data objects"
Terminology trap
In C++, a function is an entity, not a data object you hold a value inside, and a reference is an alias to an existing object rather than a standalone data object in the language model (though underlying implementations may reserve storage for it in some cases). Keeping this straight prevents confusion later when learning references, pointers to functions, and sizeof.
Practice Check
Exercise 1. For each line, say whether it compiles or fails to compile, and briefly explain why:
int a = 3;
int b = 4;
const int* p1 = &a;
*p1 = 10; // (1)
p1 = &b; // (2)
int* const p2 = &a;
*p2 = 10; // (3)
p2 = &b; // (4)Exercise 2. What does the following program print?
#include <iostream>
int main() {
int value = 5;
const int* ptr = &value;
value = 12;
std::cout << *ptr << "\n";
}Exercise 3. Match each description to the right declaration (const int* p, int* const p, or const int* const p):
- (a) You can change what
ppoints to, but you cannot change the integer through*p. - (b) You cannot change what
ppoints to, and you cannot change the integer through*p. - (c) You cannot change what
ppoints to, but you can change the integer through*p.
Solutions
Exercise 1 — compilation and explanation
- (1) Fails to compile:
p1points toconst int, so*p1cannot be assigned to. - (2) Compiles:
p1is not aconstpointer; it can be reassigned to point tob. - (3) Compiles:
p2is aconstpointer to a non-constint, so modifying*p2is allowed. - (4) Fails to compile:
p2has top-levelconst(int* const), sop2cannot be pointed atb.
Exercise 2 — output of the snippet
Output:
12Even though ptr points to const int, value itself was defined as a non-const int and was modified directly through value. Reading *ptr then reads the updated value 12 from that memory location.
Exercise 3 — matching declarations
- (a)
const int* p(low-levelconstonly) - (b)
const int* const p(both low-level and top-levelconst) - (c)
int* const p(top-levelconstonly)
Summary
- A name in source code refers to a typed region of memory that has an address, size (
sizeof), and alignment (alignof). - Every object is an entity with storage holding a value, while other entities (like functions and references) are not ordinary data objects.
constrestricts mutability along a given access path; it is not a magic runtime lock that freezes memory everywhere.- Top-level
constrestricts the entity/pointer itself; low-levelconstrestricts modifying the value through a pointer/reference to aconsttype. - Always initialize
constvariables at definition, and never dropconstimplicitly from a pointer.
Quick Reference
| Concept / Form | Meaning in Plain English | Typical Rule / Behavior |
|---|---|---|
| Identifier / Name | The spelling you write in code (x, p) | Refers to an entity in scope |
| Type | Rulebook for interpreting bits & allowed operations | Determines sizeof, alignof, and valid operators |
| Address / Size / Alignment | Where it sits, how many bytes it takes, boundary rule | Inspected via &x, sizeof(x), alignof(x) |
| Object vs. Entity | Object = value storage; Entity = broader named/identifiable construct | Functions and references are entities, not data objects |
const int x = 1; | x cannot be reassigned after initialization | Must be initialized when defined |
const int* p | Pointer to const int (low-level const) | Cannot do *p = ...; can do p = ... |
int* const p | const pointer to int (top-level const) | Can do *p = ...; cannot do p = ... |
const int* const p | const pointer to const int (both) | Cannot do *p = ... and cannot do p = ... |