Home CodingThe const Qualifier, constexpr, and the Symbol Table

The const Qualifier, constexpr, and the Symbol Table

by dnaadmin

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;p is a pointer to an int.

  • const int * p;p is a pointer to an int that is const. (The data is protected).

  • int * const p;p is a const pointer to an int. (The address is protected).

  • const int * const p;p is a const pointer to a const 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 const is usually placed in the .rodata (Read-Only Data) segment.

  • On Bare Metal: The linker maps the .rodata segment 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 const value?”

The Answer: Technically, you can const_cast it 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.

  • const means “Read-Only at Runtime.”

  • constexpr means “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 const sensor object, but you need to update a “Last Accessed” timestamp or a mutex whenever you read it. mutable allows the “internal state” to change while the “public interface” remains const.


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?

You may also like

Leave a Comment