In a C/C++ interview, the const keyword is a classic “litmus test.” A junior developer thinks const just means “I can’t change this variable.” An Architect knows that const is a powerful tool for memory optimization, security, and hardware-level mapping.
1. The const Pointer “Clock” Rule
One of the most frequent technical hurdles is deciphering pointer declarations. The trick is to read the declaration from right to left:
-
int * p;→pis a pointer to anint. -
const int * p;→pis a pointer to anintthat isconst. (The data is protected). -
int * const p;→pis aconstpointer to anint. (The address is protected). -
const int * const p;→pis aconstpointer to aconst int. (Both are protected).
2. Where does const live? (The Symbol Table)
This is the “Architect-level” follow-up. If you declare a global const int x = 100;, where does it go in the ELF binary?
-
In C: A global
constis usually placed in the.rodata(Read-Only Data) segment. -
On Bare Metal: The linker maps the
.rodatasegment directly into Flash/ROM. -
The Benefit: This saves precious RAM. If you forget the
const, the variable is copied into RAM at boot, wasting space and power.
Interview Trap: “Can you change a
constvalue?”The Answer: Technically, you can
const_castit in C++ or use a pointer in C to overwrite the memory. However, if that memory is physically located in Read-Only Flash, the CPU will trigger a Hardware Exception (Bus Fault) the moment you try to write to it.
3. constexpr vs. const (C++11 and Beyond)
For modern C++ systems, constexpr is a game-changer.
-
constmeans “Read-Only at Runtime.” -
constexprmeans “Constant at Compile-time.”
If you define constexpr int size = 10 + 5;, the compiler does the math and replaces every instance of size with 15 in the binary. This is essential for Zero-Overhead Abstractions, allowing you to perform complex calculations (like CRC tables or baud rate dividers) during compilation rather than wasting CPU cycles at boot.
4. mutable and the “Logical Const”
In C++, the mutable keyword allows a member of a const object to be modified.
-
Use Case: You have a
constsensor object, but you need to update a “Last Accessed” timestamp or a mutex whenever you read it.mutableallows the “internal state” to change while the “public interface” remainsconst.
5. Summary Table: Constancy in Systems
| Keyword | Placement | Evaluated | Use Case |
const |
.rodata (Flash) |
Runtime | Hardware addresses, version strings. |
constexpr |
Immediate/In-line | Compile-time | Array sizes, math constants, lookup tables. |
#define |
Preprocessor | Pre-compile | Legacy code (unsafe, no type checking). |
mutable |
.data / .bss |
Runtime | Thread-safety locks within const objects. |
Architect’s Interview Tip
When discussing const, mention Thread Safety. Marking a function or a parameter as const is the simplest form of documentation to tell other engineers (and the compiler) that this operation is Side-Effect Free. In a multi-core system, const data is inherently thread-safe because no one is allowed to modify it.
In the next article, we tackle the “Forbidden” zone: Interrupt Service Routines (ISRs) and their strict execution restrictions.
Ready for Article 5?
