C++ for Embedded · Interview question

Do namespaces add any runtime cost?

A strong answer

None. Namespaces are a purely compile-time naming and scoping mechanism, they affect how names are looked up and how symbols are mangled, but uart::init() compiles to exactly the same machine code as a C uart_init() would. There's no runtime lookup, no indirection, no metadata. This puts them squarely in the zero-overhead category alongside non-virtual methods, overloading, templates, and constexpr, features you use freely on an MCU because they cost nothing at runtime. The only build-time consideration is that namespaced symbols participate in name mangling, which matters when interfacing with C (you'd use extern "C" for the C-facing boundary), but that's a linkage concern, not a runtime cost.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Namespaces & Code Organization

Group related names to avoid collisions, replace C's prefix conventions, and structure firmware into headers and translation units that compile fast and read clearly.

More Namespaces & Code Organization questions

Browse all 472 interview questions